打卡题目:leetcode第402题,移掉K位数字
    相似题目:738
    image.png


    【代码】

    1. class Solution:
    2. def removeKdigits(self, num: str, k: int) -> str:
    3. # 单调栈
    4. stack = []
    5. for digit in num:
    6. while k and stack and digit < stack[-1]:
    7. stack.pop()
    8. k -= 1
    9. stack.append(digit)
    10. final_stack = stack[:-k] if k else stack
    11. return "".join(final_stack).lstrip('0') or "0"