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中的例子。
 
 class Solution(object):
def removeKdigits(self, num, k):
"""
:type num: str
:type k: int
:rtype: str
"""
n = len(num)
stack = ['']
for i in range(n):
while stack[-1] > num[i] and i - (len(stack)-1) < k: # we have put a 0 in the stack
stack.pop()
stack.append(num[i])
while len(stack)-1 > n - k:
stack.pop()
return str(int(''.join(stack)))
 

leetcode 402. Remove K Digits的更多相关文章

  1. leetcode 402. Remove K Digits 、321. Create Maximum Number

    402. Remove K Digits https://www.cnblogs.com/grandyang/p/5883736.html https://blog.csdn.net/fuxuemin ...

  2. [LeetCode] 402. Remove K Digits 去掉K位数字

    Given a non-negative integer num represented as a string, remove k digits from the number so that th ...

  3. 【LeetCode】402. Remove K Digits 解题报告(Python)

    [LeetCode]402. Remove K Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

  4. 402. Remove K Digits/738.Monotone Increasing Digits/321. Create Maximum Number

    Given a non-negative integer num represented as a string, remove k digits from the number so that th ...

  5. 【leetcode】402. Remove K Digits

    题目如下: 解题思路:我的方法是从头开始遍历num,对于任意一个num[i],在[i+1~len(num)-1]区间内找出离num[i]最近并且小于num[i]的数num[j],如果j-i <= ...

  6. 402. Remove K Digits

    (English version is after the code part) 这个题做起来比看起来容易,然后我也没仔细想,先速度刷完,以后再看有没有改进. 用这个来说: 1 2 4 3 2 2 1 ...

  7. 402 Remove K Digits 移掉K位数字

    给定一个以字符串表示的非负整数 num,移除这个数中的 k 位数字,使得剩下的数字最小.注意:    num 的长度小于 10002 且 ≥ k.    num 不会包含任何前导零.示例 1 :输入: ...

  8. [LeetCode] Remove K Digits 去掉K位数字

    Given a non-negative integer num represented as a string, remove k digits from the number so that th ...

  9. Leetcode: Remove K Digits

    Given a non-negative integer num represented as a string, remove k digits from the number so that th ...

随机推荐

  1. swift 集合类型(二)

    说到swift的集合类型,就不得不谈到Dictionary.包含一个键值对组合的集合. var air = ["name":"warner","tit ...

  2. Router的创建者——RouteBuilder

    Router的创建者--RouteBuilder 在<注册URL模式与HttpHandler的映射关系>演示的实例中,我们总是利用一个RouteBuilder对象来为RouterMiddl ...

  3. [py]文件 字符串 列表特例

    文件 readlines 列表 readline 字符串 read 字符串   列表---拆分---小列表   f=file('test.log','r') for line in f.readlin ...

  4. Java并发编程实战(使用synchronized实现同步方法)

    本文介绍java最基本的同步方式,即使用synchronized关键字来控制一个方法的并发访问,如果一个对象已用synchronized关键字声明,那么只有一个执行线程允许去访问它,其它试图访问这个对 ...

  5. Python2.5-原理之模块

    此部分来自于<Python学习手册>第五部分 一.模块(21章) 模块是最高级别的程序组织单元,它将程序代码和数据封装起来以便重用..模块往往对应于python程序文件.每个文件就是一个模 ...

  6. 使用Jekyll在Github上搭建博客

    最近在玩github,突然发现很多说明网站或者一些介绍页面全部在一个域名是*****.github.io上. 好奇!!!真的好奇!!!怎么弄的?我也要一个~~~ 于是去网站上查询了一下,找到了http ...

  7. 两道关于JS的小考题(闭包与中间件)

    题目一:写一个javascript函数 calculate,该函数有如下性质 calculate() = 0; calculate(2)() = 2; calculate(3)(4)(1)(5)() ...

  8. jQuery api 快速参考[转]

    选择符 匹配 * 所有元素 #id 带有给定ID的元素 element 给定类型的所有元素,比如说html标签 .class 带有给定类的所有元素 a,b 匹配a或者匹配b的元素 a b 作为a后代的 ...

  9. 【Magenta 项目初探】手把手教你用Tensorflow神经网络创造音乐

    原文链接:http://www.cnblogs.com/learn-to-rock/p/5677458.html 偶然在网上看到了一个让我很感兴趣的项目 Magenta,用Tensorflow让神经网 ...

  10. 52-which 显示系统命令所在目录

    显示系统命令所在目录 which command-list 参数 command-list 是which搜索的一条或多条命令(实用程序) 示例 which 单条命令 $ which ls /bin/l ...