6. Remove K Digits

Link to the problem

Description

Given a non-negative integer num represented as a string, remove k digits from the number so that the new number is the smallest possible.

Note:

  • The length of num is less than 10002 and will be ≥ k.
  • The given num does not contain any leading zero.

Example

Input: num = "1432219", k = 3, Output: "1219"
Remove the three digits 4, 3, and 2 to form the new number 1219 which is the smallest.
Input: num = "10200", k = 1, Output: "200"
Remove the leading 1 and the number is 200. Note that the output must not contain leading zeroes.
Input: num = "10", k = 2, Output: "0"
Remove all the digits from the number and it is left with nothing which is 0.

Idea

Greedy algorithm works in this case. Suppose x > y, and we want to remove one digit from x, then we can move the same digit in y, and get x' >= y'. Hence, it never hurts to remove one digit which make the number smallest.
To remove a single digit to make the remaining number smallest, we remove the first digit, whose right neighbor is strictly smaller than itself.

Solution

class Solution {
private:
    string removeOneDigit(string num) {
        int index = 0;
        while (index < num.size() - 1 && num[index + 1] >= num[index]) {
            index++;
        }
        return num.substr(0, index) + num.substr(index + 1);
    }
public:
    string removeKdigits(string num, int k) {
        for (int i = 0; i < k; i++) {
            num = removeOneDigit(num);
        }
        int index = 0;
        while (index < num.size() && num[index] == '0') index++;
        num = num.substr(index);
        if (num.empty()) num = "0";
        return num;
    }
};

Performance

33 / 33 test cases passed.
Runtime: 16 ms

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末琼梆,一起剝皮案震驚了整個(gè)濱河市七咧,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌叮叹,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,807評(píng)論 6 518
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件爆存,死亡現(xiàn)場(chǎng)離奇詭異蛉顽,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)先较,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,284評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門携冤,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人闲勺,你說(shuō)我怎么就攤上這事曾棕。” “怎么了菜循?”我有些...
    開封第一講書人閱讀 169,589評(píng)論 0 363
  • 文/不壞的土叔 我叫張陵翘地,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我癌幕,道長(zhǎng)衙耕,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,188評(píng)論 1 300
  • 正文 為了忘掉前任勺远,我火速辦了婚禮橙喘,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘胶逢。我一直安慰自己厅瞎,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,185評(píng)論 6 398
  • 文/花漫 我一把揭開白布初坠。 她就那樣靜靜地躺著和簸,像睡著了一般。 火紅的嫁衣襯著肌膚如雪某筐。 梳的紋絲不亂的頭發(fā)上比搭,一...
    開封第一講書人閱讀 52,785評(píng)論 1 314
  • 那天,我揣著相機(jī)與錄音南誊,去河邊找鬼身诺。 笑死,一個(gè)胖子當(dāng)著我的面吹牛抄囚,可吹牛的內(nèi)容都是我干的霉赡。 我是一名探鬼主播,決...
    沈念sama閱讀 41,220評(píng)論 3 423
  • 文/蒼蘭香墨 我猛地睜開眼幔托,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼穴亏!你這毒婦竟也來(lái)了蜂挪?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,167評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤嗓化,失蹤者是張志新(化名)和其女友劉穎棠涮,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體刺覆,經(jīng)...
    沈念sama閱讀 46,698評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡严肪,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,767評(píng)論 3 343
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了谦屑。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片驳糯。...
    茶點(diǎn)故事閱讀 40,912評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖氢橙,靈堂內(nèi)的尸體忽然破棺而出酝枢,到底是詐尸還是另有隱情,我是刑警寧澤悍手,帶...
    沈念sama閱讀 36,572評(píng)論 5 351
  • 正文 年R本政府宣布帘睦,位于F島的核電站,受9級(jí)特大地震影響坦康,放射性物質(zhì)發(fā)生泄漏官脓。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,254評(píng)論 3 336
  • 文/蒙蒙 一涝焙、第九天 我趴在偏房一處隱蔽的房頂上張望卑笨。 院中可真熱鬧,春花似錦仑撞、人聲如沸赤兴。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,746評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)桶良。三九已至,卻和暖如春沮翔,著一層夾襖步出監(jiān)牢的瞬間陨帆,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,859評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工采蚀, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留疲牵,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 49,359評(píng)論 3 379
  • 正文 我出身青樓榆鼠,卻偏偏與公主長(zhǎng)得像纲爸,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子妆够,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,922評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容

  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問(wèn)題, 分享了一些自己做題目的經(jīng)驗(yàn)识啦。 張土汪:刷leetcod...
    土汪閱讀 12,749評(píng)論 0 33
  • 今天去了健身房 雖然聽起來(lái)沒(méi)什么 但是在這個(gè)人生地不熟的城市 只是憑著一張美團(tuán)單次健身卡就踏上旅程 坐著沒(méi)做過(guò)的公...
    神投手閱讀 162評(píng)論 2 1
  • 今日早餐负蚊,主角:扯面條,配角:葡萄颓哮、牛奶家妆、玫瑰花花 叨叨念:在北方的大城小城里,總會(huì)找到各種各樣的面館冕茅,賣著各式各...
    櫻櫻小香閱讀 474評(píng)論 2 4
  • 今天給大家分享一下個(gè)人關(guān)于鏡子理論的看法 1 你親手創(chuàng)造了你的世界 你希望它是什么樣的揩徊,它將會(huì)往你希望的方向走, ...
    滔Roy閱讀 2,146評(píng)論 0 46