0.原题
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:
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.
1.程序
class Solution:
def removeKdigits(self, num, k):
"""
:type num: str
:type k: int
:rtype: str
"""
result = self.fun(num,k)
if result == '':
result = '0'
else:
result = str(int(result))
return result
def fun(self,num,k):
if k == 0:
return num
else:
length = len(num)
for i in range(length-1):
if num[i] > num[i+1]:
new_num = num[:i] + num[i+1:]
result = self.fun(new_num,k-1)
return result
new_num = num[:length-1]
result = self.fun(new_num,k-1)
return result
if __name__ == '__main__':
num = '10'
k = 2
solution = Solution()
result = solution.removeKdigits(num,k)
print(result)
2.解题思路
本题中最大数字长度为10002,这显然是一个非常大的数,如果使用遍历暴力求解,当然不合适。
因此,本题的重点在于要找到数字Remove的最佳方法。
首先,我们根据常识可知:
再看一个例子,我们从左向右,尝试依次去掉一位:
当我们只去掉一位的时候,从左往右查找,每次查找相邻的两个数字:
那么,如果去掉多个数字怎么办呢?
如果每次去掉一位,并保证去掉数字后得到的数是最小的,那么通过递归,得到的数也必定是最小的。
结论:
我们从左向右查找:
如果前一位比后一位大,那么去掉前一位,得到最小的数。
如果每一位都比后一位小,去掉最后一位,得到最小的数。