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

标签(空格分隔): LeetCode

作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/remove-k-digits/description/

题目描述:

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个数字,使得剩下来的数字字符串是最小的。

解题方法

看了Topics知道这个是用栈来解决的题目。看了别人的解答才明白怎么回事。

使用一个栈作为辅助,遍历数字字符串,当当前的字符比栈最后的字符小的时候,说明要把栈的最后的这个字符删除掉。为什么呢?你想,把栈最后的字符删除掉,然后用现在的字符进行替换,是不是数字比以前的那种情况更小了?所以同样的道理,做一个while循环!这个很重要,可是我没有想到。在每一个数字处理的时候,都要做一个循环,使得栈里面最后的数字比当前数字大的都弹出去。

最后,如果K还没用完,那要删除哪里的字符呢?毋庸置疑肯定是最后的字符,因为前面的字符都是小字符。

很有意思的一个题目!

代码如下:

class Solution(object):
def removeKdigits(self, num, k):
"""
:type num: str
:type k: int
:rtype: str
"""
if len(num) == k:
return '0'
stack = []
for n in num:
while stack and k and int(stack[-1]) > int(n):
stack.pop()
k -= 1
stack.append(n)
while k:
stack.pop()
k -= 1
if not stack:
return '0'
return str(int("".join(stack)))

日期

2018 年 7 月 13 日 ———— 早起困一上午,中午必须好好休息才行啊

【LeetCode】402. Remove K Digits 解题报告(Python)的更多相关文章

  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

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

  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】 258. Add Digits 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:减1模9 方法三:直接模9 日 ...

  6. 【leetcode】402. Remove K Digits

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

  7. 【LeetCode】788. Rotated Digits 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. 402. Remove K Digits

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

  9. 【LeetCode】62. Unique Paths 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...

随机推荐

  1. 半主机模式和_MICROLIB 库

    半主机是这么一种机制,它使得在ARM目标上跑的代码,如果主机电脑运行了调试器,那么该代码可以使用该主机电脑的输入输出设备.   这点非常重要,因为开发初期,可能开发者根本不知道该 ARM 器件上有什么 ...

  2. Netty | 第1章 Java NIO 网络编程《Netty In Action》

    目录 前言 1. Java 网络编程 1.1 Javs NIO 基本介绍 1.2 缓冲区 Buffer 1.2 通道 Channel 1.3 选择器 Selector 1.4 NIO 非阻塞网络编程原 ...

  3. 去空格及换行制表符【c#】

    string returnStr = tbxContractNO.Text.Replace("\n", "").Replace(" ", & ...

  4. 一站式Flink&Spark平台解决方案——StreamX

    大家好,我是独孤风.今天为大家推荐的是一个完全开源的项目StreamX.该项目的发起者Ben也是我的好朋友. ****什么是StreamX,StreamX 是Flink & Spark极速开发 ...

  5. 巩固javaweb的第二十七天

    巩固内容 正则表达式: 5. 指定字符串的开始和结尾 正则表达式中字符串的开始和结束符如表 2.6 所示. 表 2.6 开 始 和 结 尾 字符 作 用 ^ 指定以某个字符串开始 $ 指定以某个字符串 ...

  6. 日常Java 2021/10/24

    Java ArrrayList ArrayList类是一个可以动态修改的数组,没有固定大小的限制,可以在任何时候添加或者删除元素 ArrayList类在java.util包中使用之前需要引用 E:泛型 ...

  7. Netty之Channel*

    Netty之Channel* 本文内容主要参考**<<Netty In Action>> ** 和Netty的文档和源码,偏笔记向. 先简略了解一下ChannelPipelin ...

  8. Angular中@Output()的使用方法

    子component中的html文件 <button (click)="Send()">送出</button><br> 子component中的 ...

  9. 我在项目中是这样配置Vue的

    启用压缩,让页面加载更快 在我们开发的时候,为了方便调试,我们需要使用源码进行调试,但在生产环境,我们追求的更多的是加载更快,体验更好,这时候我们会将代码中的空格注释去掉,对代码进行混淆压缩,只为了让 ...

  10. Ecshop 后台导出订单Excel时, 内存溢出的解决方法

    今天继续跟大家分享一下,在我配置Ecshop时的问题. 今天的问题是在后台想要导出订单列表Excel时出现的内存溢出.错误提示如下 问题:  Fatal error: Allowed memory s ...