当前位置: 代码迷 >> 综合 >> LeetCode 402 移调K位数字
  详细解决方案

LeetCode 402 移调K位数字

热度:32   发布时间:2024-02-21 02:39:05.0

给定一个以字符串表示的非负整数 num,移除这个数中的 k 位数字,使得剩下的数字最小。

class Solution:def removeKdigits(self, num: str, k: int) -> str:stack=[]remain_num=len(num)-kfor c in num:while k and stack and ord(stack[-1])>ord(c):stack.pop()k-=1stack.append(c)return ''.join(stack[:remain_num]).lstrip('0') or '0'