-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove_k_digits.py
More file actions
54 lines (46 loc) · 1.72 KB
/
Copy pathremove_k_digits.py
File metadata and controls
54 lines (46 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
## LeetCode 402. Remove K Digits
# https://leetcode.com/problems/remove-k-digits/
class Solutions:
def removeKdigits(self, num, k):
size = len(str(num))
if k == size:
return "0"
# Store the final string in stack
counter = 0
mystack = []
while counter < size:
while mystack and k > 0 and mystack[-1] > num[counter]:
# When we find the number that is less that the peak of the stack
mystack.pop()
k -= 1
if num[counter] != "0":
print("add new num[counter] to the stack")
print(num[counter])
mystack.append(num[counter])
counter += 1
print(counter, size)
else:
print("num is zero")
print(num[counter])
counter += 1
k -= 1
# Now remove the largest values from the top of the stack
while mystack and k > 0:
mystack.pop()
k -= 1
# If the stack is not empty, pop the number on the right hand side
return "".join(mystack)
if __name__ == "__main__":
solution = Solutions()
print("first")
# Remove the three digits 4, 3, and 2 to form the new number 1219 which is the smallest.
first = solution.removeKdigits("1432219", 3)
print(first)
print("second")
# Remove the leading 1 and the number is 200. Note that the output must not contain leading zeroes.
second = solution.removeKdigits("10200", 1)
print(second)
print("third")
# Remove all the digits from the number and it is left with nothing which is 0.
third = solution.removeKdigits("10", 2)
print(third)