思路:
用ans栈来表示结果
每次遍历如果第i位小于ans栈顶,说明删掉栈顶数字会变小,然后把第i位压入栈
class Solution:
def removeKdigits(self, num: str, k: int) -> str:
ans=[num[0]]
for i in range(1,len(num)):
while (len(ans)!=0 and num[i]<ans[-1] and k):
k-=1
ans.pop()
ans.append(num[i])
while(k):
ans.pop()
k-=1
if ans==[]:return "0"
return str(int("".join(ans)))