Leetcode刷题98-402. 移掉K位数字(C++详细解法!!!)

Come from : [https://leetcode-cn.com/problems/remove-k-digits/]

1.Question

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:

1. The length of num is less than 10002 and will be ≥ k.
2. 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.

2.Answer

meduim 类型题目。贪心算法。
算法分析

  1. 使用存储最终结果或删除工作,从高位向低位遍历num。
  2. 如果遍历的数字大于栈顶元素,则将该数字push入栈
  3. 如果小于栈顶元素则进行pop弹栈,知道栈为空或者不能删除数字(k==0)或栈顶小于当前元素为止。

弹出示例:
在这里插入图片描述

思考,需要特殊处理的部分:(特殊处理的部分全部在AC代码中给出)

  1. 当所有数字都扫描完成后,k仍然>0,应该如何处理?比如:num=12345,k=3时
  2. 当数字中有0出现,应该有怎样的特殊处理?比如:num=100200,k=0时
  3. 如何将最后结果存储为字符串并返回?
    AC代码如下:
class Solution {
public:
    string removeKdigits(string num, int k) {
        vector<int> S;
        string result = "";     //存储最终结果
        for(int i = 0; i < num.length(); ++i)
        {
            int number = num[i] - '0';  //将字符转化为整型使用
            while(S.size() > 0 && number < S[S.size()-1] && k > 0)//当栈不空,栈顶元素大于number 且k>0时可以删除数字
            {
                S.pop_back();  //弹出栈顶元素
                --k;
            }
            if(S.size() != 0 || number != 0)  //number不等于0直接入栈,或number等于0 但 栈的元素 大于0,也可入栈
            {
                S.push_back(number);    //将数字number压入栈中
            }
        }
        while(S.size() != 0 && k > 0)   //如果栈不空,且 仍然可以删除数字
        {
            S.pop_back();
            --k;                  //删除后更新k
        }
        for(int i = 0; i < S.size(); ++i) //将栈中的元素从头遍历,存储至result中
        {
            result.append(1, '0'+S[i]);   //‘0’+S[i] 数字转字符
        }
        if(result == "")
        {
            result = "0";                //若result为空,则返回0
        }
        return result;
    }
};

3.大神的算法

大神AC速度第一代码:
直接借助栈数据结构。也没有用到字符转换成数字的情况。

class Solution {
public:
    string removeKdigits(string num, int k) {
        if(num==" ") return "0";
        if(k==0) return num;
        stack<char> stk;
        stack<char> tmp;
        stk.push(num[0]);
        string res="";
        for(int i=1;i<num.length();i++)
        {
            if(num[i]>=stk.top() || k==0)
                stk.push(num[i]);
            else 
            {
                while(!stk.empty() && num[i]<stk.top() && k>0)
                {
                    stk.pop();
                    k--;
                }
            stk.push(num[i]);
            }
            
                
        }
        while(k>0)
        {
            stk.pop();
            k--;
        }
        while(!stk.empty())
        {
            tmp.push(stk.top());
            stk.pop();
        }
        while(!tmp.empty()&&'0'==tmp.top())
        {
            tmp.pop();
        }
        while(!tmp.empty())
            {
            res+=tmp.top();
            tmp.pop();
        }
        if(res=="") return "0";
        return res;
        
    }
};

4.我的收获

贪心算法初步。。。
fighting。。。

2019/6/10 胡云层 于南京 98

猜你喜欢

转载自blog.csdn.net/qq_40858438/article/details/91405286
今日推荐