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. Python的高级特性9:蹩脚的多态

    学习了java再来看python的多态,总感觉怪怪的,很蹩脚.. 1.python的父类根本不能调用子类的方法,只能蹩脚的依靠重写方法,然后在运行时去调用,实现伪多态... 2.所谓的鸭子类型看起来很 ...

  2. Inline Workers--Web workers without a separate Javascript file

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> </head&g ...

  3. ASP.NET性能优化之减少请求

    在上篇<ASP.NET性能优化之让浏览器缓存动态网页> 中的方案中,浏览器发送If-Modified-Since将是否需要使用自己的缓存交给WEB服务器去决定,服务器告知浏览器去读缓存,浏 ...

  4. 清北学堂2017NOIP冬令营入学测试P4749 C’s problem(c)

    P4746 C's problem(c) 时间: 1000ms / 空间: 655360KiB / Java类名: Main 背景 冬令营入学测试 描述 题目描述 小C是一名数学家,由于它自制力比较差 ...

  5. AsyncTask源码解析

    package com.example.demo.activity.net; import java.util.ArrayDeque; import java.util.concurrent.Bloc ...

  6. PAT1078 Hashing

    11-散列2 Hashing   (25分) The task of this problem is simple: insert a sequence of distinct positive in ...

  7. 拿到阿里,网易游戏,腾讯,smartx的offer的过程

    前言 从今年的3月14日阿里的电话面试开始,到现在4月16日在西安悦豪酒店进行的腾讯HR面到现在一个多月了,中间先后收到了阿里,网易游戏,腾讯和smartx的offer,今天早晨刚刚接到了腾讯HR的电 ...

  8. 综合使用spring cloud技术实现微服务应用

    在之前的章节,我们已经实现了配置服务器.注册服务器.微服务服务端,实现了服务注册与发现.这一章将实现微服务的客户端,以及联调.实现整个spring cloud框架核心应用. 本文属于<7天学会s ...

  9. nios II--实验5——定时器软件部分

    软件开发 首先,在硬件工程文件夹里面新建一个software的文件夹用于放置软件部分:打开toolsàNios II 11.0 Software Build Tools for Eclipse,需要进 ...

  10. XML的解析和保存

    1.XML(extensible markup language;XML )  定义:,可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言.     XML语法规范:  标 ...