402. Remove K Digits

本文详细解析了移除K位数以获得最小可能数值的算法,通过使用单调栈方法,介绍了如何从一个非负整数中移除指定数量的数字,使剩余数字构成的数尽可能小。文章提供了具体的实现代码和示例,如输入1432219和k=3,输出1219。同时,讨论了算法中的易错点,如处理单调递增序列和去除前缀零。


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 1:

Input: num = "1432219", k = 3
Output: "1219"
Explanation: Remove the three digits 4, 3, and 2 to form the new number 1219 which is the smallest.

Example 2:

Input: num = "10200", k = 1
Output: "200"
Explanation: Remove the leading 1 and the number is 200. Note that the output must not contain leading zeroes.

Example 3:

Input: num = "10", k = 2
Output: "0"
Explanation: Remove all the digits from the number and it is left with nothing which is 0.

方法1: monotonic stack

水中的鱼:http://fisherlei.blogspot.com/2017/07/leetcode-remove-k-digits-solution.html

思路:

易错点:

  1. 如果string本身为increasing序列,单调栈算法是舍不掉任何数的:最后要再检查一下有没有将k个数字除干净,如果没有,我们已知结果是个单调数,从后面开始pop较大值。
  2. 最后要去掉前缀0。
  3. 如果为空返回“0”。
  4. 单调栈算法再pop的时候一定要随时查空
class Solution {
public:
    string removeKdigits(string num, int k) {
    	// if need to remove all
        if (k == num.size()) {
            return "0";
        }
        stack<char> stk;
        
        for (char c : num) {
        	// keep popping while having better choice
        	// maintain an increasing sequence in the stack
            while (k && !stk.empty() && stk.top() > c) {
                stk.pop();
                k--;
            }
            stk.push(c);
        }
        // if haven't used up the k,delete from the end (large ones)
        // corner case like "1111"
        while (k) {
            stk.pop();
            k--;
        }
        // construct the number from the stack
        string ans;
        while (!stk.empty()) {
            ans += stk.top();
            stk.pop();
        }
        //remove all the 0 at the head
        while (ans.size() > 1 && ans.back() == '0') {
            ans.pop_back();
        }
        reverse(ans.begin(), ans.end());
        return ans;
    }
};

grandyang: https://www.cnblogs.com/grandyang/p/5883736.html

class Solution {
public:
    string removeKdigits(string num, int k) {
        string result = "";
        int n = num.size(), keep = n - k;
        for (char c : num) {
            while (k && result.size() && result.back() > c) {
                result.pop_back();
                k--;
            }
            result.push_back(c);
        }
        result.resize(keep);
        while (!result.empty() && result[0] == '0') 
            result.erase(result.begin());
        return result.empty() ? "0" : result;
    }
};

二刷:

class Solution {
public:
    string removeKdigits(string num, int k) {
        string res = "";
        for (char c: num) {
            while (k > 0 && res.size() && res.back() > c) {
                res.pop_back();
                k--;
            }
            res.push_back(c);
        }
        
        while (k-- > 0) res.pop_back();
        
        while (!res.empty() && *res.begin() == '0') res.erase(res.begin());
        return res.empty() ? "0" : res;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值