leetcode 402. Remove K Digits
Given a non-negative integer num represented as a string, remove k digits from the number so that the new number is the smallest possible.
Note:
- The length of num is less than 10002 and will be ≥ k.
- The given num does not contain any leading zero.
Example 1:
Input: num = "1432219", k = 3 Output: "1219" Explanation: Remove the three digits 4, 3, and 2 to form the new number 1219 which is the smallest.
Example 2:
Input: num = "10200", k = 1 Output: "200" Explanation: Remove the leading 1 and the number is 200. Note that the output must not contain leading zeroes.
Example 3:
Input: num = "10", k = 2 Output: "0" Explanation: Remove all the digits from the number and it is left with nothing which is 0.
本题的解法是拿掉k个数字,使得num中的数字尽量从小到大排列。
比如例子 '4567321', 如果之拿掉一个数字,最小的情况应当是拿掉7。也就是说如果出现 num[i] > num[i+1] 的情况,那么这个num[i]就是应当被拿掉的。解法可以维护一个stack。逐个元素推入stack。如果出现要推入的元素小于stack[-1]的情况,就要把已经推入stack的元素pop出来。
注意L11的while, 这里不能是if, 因为 k > 1时,我们需要继续pop栈底的元素,比如这个例子中,在拿掉7之后,我们应该再拿掉6。而且我们要注意查看到目前为止已经拿掉了多少个数字,只有当已经拿掉的数字个数小于k时, 才有必要继续pop。i + 1 - len(stack) < k。
L14: 如果for循环结束后,pop的数字个数还不够,应该继续pop。这里由于在L9我们先加入了一个'0',所以 len(stack) > n - k + 1。加入这个零的好处是可以避免最后join一个空堆而导致报错,比如Example 3中的例子。
1 class Solution(object): 2 def removeKdigits(self, num, k): 3 """ 4 :type num: str 5 :type k: int 6 :rtype: str 7 """ 8 n = len(num) 9 stack = ['0'] 10 for i in range(n): 11 while stack[-1] > num[i] and i - (len(stack)-1) < k: # we have put a 0 in the stack 12 stack.pop() 13 stack.append(num[i]) 14 while len(stack)-1 > n - k: 15 stack.pop() 16 return str(int(''.join(stack)))