【LeetCode】402. Remove K Digits 解题报告(Python)
【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)的更多相关文章
- 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 ...
- [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 ...
- leetcode 402. Remove K Digits
Given a non-negative integer num represented as a string, remove k digits from the number so that th ...
- 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 ...
- 【LeetCode】 258. Add Digits 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:减1模9 方法三:直接模9 日 ...
- 【leetcode】402. Remove K Digits
题目如下: 解题思路:我的方法是从头开始遍历num,对于任意一个num[i],在[i+1~len(num)-1]区间内找出离num[i]最近并且小于num[i]的数num[j],如果j-i <= ...
- 【LeetCode】788. Rotated Digits 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 402. Remove K Digits
(English version is after the code part) 这个题做起来比看起来容易,然后我也没仔细想,先速度刷完,以后再看有没有改进. 用这个来说: 1 2 4 3 2 2 1 ...
- 【LeetCode】62. Unique Paths 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
随机推荐
- C#点击按钮添加标签
<asp:Button ID="button1" runat="server" Text="创建" onclick="But ...
- day06 HTTP协议
day06 HTTP协议 HTTP协议 什么是http? HTTP 全称:Hyper Text Transfer Protocol 中文名:超文本传输协议 是一种按照URL指示,将超文本文档从一台主机 ...
- 通信协议 HTTP TCP UDP
TCP HTTP UDP: 都是通信协议,也就是通信时所遵守的规则,只有双方按照这个规则"说话",对方才能理解或为之服务. TCP HTTP UDP三者的关系: T ...
- SQL Server 和 Oracle 以及 MySQL 数据库
推荐:https://www.zhihu.com/question/19866767 三者是目前市场占有率最高(依安装量而非收入)的关系数据库,而且很有代表性.排行第四的DB2(属IBM公司),与Or ...
- 封装一个按Key排序的Map工具
Map是集合的存放顺序是按哈希值定的,有时候不是我们需要的,当想要一个按自己规定顺序存放顺序,可以用LinkedHashMap,这里自己把LinkedHashMap封装了一次 package test ...
- 在html页面通过绝对地址显示图片
1.编辑tomcat中conf目录下的server.xml文件,在<Host></Host>中添加如下代码段 <Context path="/D" d ...
- Spring中Bean的装配方式
一.基于xml的装配 Student.java package com.yh; public class Student implements People { public void breath( ...
- java中的++i是线程安全的吗?
java中的++i是线程安全的吗?为什么?怎么使它线程安全呢? 先说答案: 非线程安全 先说下为什么是非线程安全的? 从Java内存模型说起 Java内存模型规定了所有的便利都存储在主内存中,每个线程 ...
- 使用frp进行内网穿透,实现ssh远程访问Linux服务器
搭建一个完整的frp服务链需要: VPS一台(也可以是具有公网IP的实体机) 访问目标设备(就是你最终要访问的设备) 简单的Linux基础(如果基于Linux配置的话) 我这里使用了腾讯云服务器作为服 ...
- linux重启后JDk环境变量配置失效最终解决方案
最终解决方案:https://bbs.deepin.org/forum.php?mod=viewthread&tid=147762 其实这个修改可能也存在问题,如果有耐心的可以每次打开终端 ...