Skip to content

Latest commit

 

History

History
29 lines (22 loc) · 669 Bytes

402.-remove-k-digits.md

File metadata and controls

29 lines (22 loc) · 669 Bytes

402. Remove K Digits

{% tabs %} {% tab title="Python-2020" %}

class Solution:
    def removeKdigits(self, num: str, k: int) -> str:
        if k == len(num): return "0"
        
## 其实还是要自信点, 直接写的就是正确的方法, 0(n)
        stack = []
        for i,c in enumerate(num):
            while stack and stack[-1] > c and k:
                stack.pop()
                k -= 1
            stack.append(c)
        ans = "".join(stack).lstrip("0") 
        if not k:
            
        
            return ans if ans else "0"
        else:
            return ans[:-k]
            
            

{% endtab %} {% endtabs %}