Skip to content

Commit d305865

Browse files
add 0402-remove-k-digits.py
1 parent a058fb4 commit d305865

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

Diff for: python/0402-remove-k-digits.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def removeKdigits(self, num: str, k: int) -> str:
3+
stack = []
4+
for i in num:
5+
while stack and stack[-1] > i and k > 0:
6+
k -= 1
7+
stack.pop()
8+
if stack or i is not "0":
9+
stack.append(i)
10+
if k:
11+
stack = stack[:-k]
12+
return ''.join(stack) or '0'
13+

0 commit comments

Comments
 (0)