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. 通俗理解T检验和F检验

    来源: http://blog.sina.com.cn/s/blog_4ee13c2c01016div.html   1,T检验和F检验的由来 一般而言,为了确定从样本(sample)统计结果推论至总 ...

  2. nginx架构

    nginx平台初探(100%)  

  3. Centos 7 安装jdk 配置环境变量

    在Centos7 终端中,我们输入java -version可以看到java的版本,但是输入javac却没有反应 原因是系统中预装的是openjdk jre不是真正的jdk,所以还得自己装好,然后配置 ...

  4. Spring Security笔记:解决CsrfFilter与Rest服务Post方式的矛盾

    基于Spring Security+Spring MVC的web应用,为了防止跨站提交攻击,通常会配置csrf,即: <http ...> ... <csrf /> </ ...

  5. html文本标准模式,首行空两格,两端对齐,行高

    font-size: 13px; line-height: 1.6; text-align: justify; text-indent: 2em;

  6. .NET基于Redis缓存实现单点登录SSO的解决方案

    一.基本概念 最近公司的多个业务系统要统一整合使用同一个登录,这就是我们耳熟能详的单点登录,现在就NET基于Redis缓存实现单点登录做一个简单的分享. 单点登录(Single Sign On),简称 ...

  7. 创业这三年¥.NET之尴尬处境

    创业这三年#迈出第一步 创业这三年@各种奇遇 之前写的文章有兴趣的大家可以看看. 本来没有打算写这样一篇会遭人拍砖的文章,但是发现大家每天忙于编码,对市场环境..Net生态没有一个真实.多角度的认识, ...

  8. Scala入门之函数

    /** * 函数可以被简单的被认为是包裹了一条或者几条语句的代码体,该代码体接收若干参数,经过代码体处理后返回结果,形如数学中的f(x) = x + 1 * 在Scala中函数式一等公民,可以向变量一 ...

  9. 2015/11/9用Python写游戏,pygame入门(8):按钮和游戏结束

    昨天没有更新内容,今天相对多写一些. 因为我们已经基本完成游戏框架,但是游戏结束后,并不知道怎样比较好开始.我本来本着懒的原则,想结束后显示一个黑屏,然后你重新点一下鼠标就重新开始.但是那样实在太不像 ...

  10. Verilog代码规范I

    Verilog代码规范I "规范"这问题 "规范"这个富含专业气息的词汇(个人感觉),其实规范这种东西,就是大家都约定熟成的东西,一旦你不遵守这个东西,专业人士 ...