-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path402.py
42 lines (38 loc) · 1.3 KB
/
402.py
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
__________________________________________________________________________________________________
sample 32 ms submission
#
# @lc app=leetcode id=402 lang=python3
#
# [402] Remove K Digits
#
class Solution:
def removeKdigits(self, num: str, k: int) -> str:
out = []
for c in num:
while k > 0 and out and out[-1] > c:
out.pop()
k -= 1
out.append(c)
return ''.join(out[:-k or None]).lstrip('0') or '0'
__________________________________________________________________________________________________
sample 13060 kb submission
class Solution:
def removeKdigits(self, num: str, k: int) -> str:
if k >= len(num):
return '0'
changed = False
while k > 0:
for i in range(len(num) - 1):
if num[i] > num[i+1]:
num = num[:i] + num[i+1:]
changed = True
break
if not changed:
num = num[:-1]
k -= 1
changed = False
i = 0
while i < len(num) - 1 and num[i] == '0':
i += 1
return num[i:]
__________________________________________________________________________________________________