402. Remove K Digits

2016-12-16  本文已影响0人  阿团相信梦想都能实现
class Solution(object):
    def removeKdigits(self, num, k):
        """
        :type num: str
        :type k: int
        :rtype: str
        """
       
        stack=[]
        for d in num:
            while stack and k>0 and stack[-1]>d:
                stack.pop()
                k-=1
            stack.append(d)
        return ''.join(stack[:-k or None]).lstrip('0') or '0'
        
            
上一篇 下一篇

猜你喜欢

热点阅读