注意事项

贪心算法:从左到右遍历数字,尽量移除较大的数字。栈:使用栈来存储当前的最小数字序列。

前导零:结果中不能有前导零。

#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param num string字符串 
# @param k int整型 
# @return string字符串
#
class Solution:
    def removeKnums(self , num: str, k: int) -> str:
        # write code here
        s = []
        for x in num:
            while k and s and s[-1]>x:#尽可能的删除掉前面的大的数字
                s.pop()
                k -= 1
            s.append(x)
        s = s[:-k] if k else s#如果k位尚未全部移除,则需要在最后截取
        ans = ''.join(s).lstrip('0')#注意前导0字符的删除
        return ans if ans else 0#全部被删除或只剩前导0字符,则返回0