402. Remove K Digits

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)