美文网首页
402. Remove K Digits

402. Remove K Digits

作者: 阿团相信梦想都能实现 | 来源:发表于2016-12-16 15:24 被阅读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'
        
            

相关文章

网友评论

      本文标题:402. Remove K Digits

      本文链接:https://www.haomeiwen.com/subject/gomnmttx.html