-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path402.py
36 lines (27 loc) · 805 Bytes
/
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = ['"wuyadong" <[email protected]>']
class Solution(object):
def removeKdigits(self, num, k):
"""
:type num: str
:type k: int
:rtype: str
"""
min_num_list = []
n = len(num)
start = 0
left_k = n - k
while left_k > 0:
min_num = num[start]
min_i = start
for i in xrange(start+1, n-left_k+1):
if min_num > num[i]:
min_num = num[i]
min_i = i
min_num_list.append(min_num)
left_k -= 1
start = min_i+1
return "".join(min_num_list).lstrip('0') or "0"
if __name__ == "__main__":
print Solution().removeKdigits("10", 2)