LeetCode-Microsoft-Remove K Digits
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.
思路:其基本思想是利用栈尽量维持一个递增的序列,也就是说将字符串中字符依次入栈,如果当前字符串比栈顶元素小,并且还可以继续删除元素,那么就将栈顶元素删掉,这样可以保证将当前元素加进去一定可以得到一个较小的序列.也可以算是一个贪心思想.最后我们只取前len-k个元素构成一个序列即可,如果这样得到的是一个空串那就手动返回0.还有一个需要注意的是字符串首字符不为0
class Solution {
public String removeKdigits(String num, int k) {
if(num == null || num.length() == 0){
return null;
}
Stack<Integer> stack = new Stack<>(); for(int i = 0; i<num.length(); i++){
int cur = num.charAt(i) - '0';
while(!stack.isEmpty() && cur < stack.peek() && num.length() - i - 1 >= num.length()-k-stack.size()){
stack.pop();
}
if(stack.size() < num.length()-k){
stack.push(cur);
}
}
StringBuilder res = new StringBuilder();
int count = 0;
while (!stack.isEmpty()){
res.insert(0, stack.pop());
} while (res.length() > 0 && res.charAt(0) == '0'){
res.deleteCharAt(0);
} if(res.length() == 0){
return "0";
}
return res.toString();
}
}
LeetCode-Microsoft-Remove K Digits的更多相关文章
- [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 ...
- 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] 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 解题报告(Python)
[LeetCode]402. Remove K Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- Leetcode: 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 ...
- [Swift]LeetCode402. 移掉K位数字 | Remove K Digits
Given a non-negative integer num represented as a string, remove k digits from the number so that th ...
- Remove K Digits
Given string A representative a positive integer which has N digits, remove any k digits of the numb ...
- 【leetcode】402. Remove K Digits
题目如下: 解题思路:我的方法是从头开始遍历num,对于任意一个num[i],在[i+1~len(num)-1]区间内找出离num[i]最近并且小于num[i]的数num[j],如果j-i <= ...
随机推荐
- 雷林鹏分享:C# 事件(Event)
C# 事件(Event) 事件(Event) 基本上说是一个用户操作,如按键.点击.鼠标移动等等,或者是一些出现,如系统生成的通知.应用程序需要在事件发生时响应事件.例如,中断.事件是用于进程间通信. ...
- Asp.Net MVC中Action跳转
首先action的跳转大致归类: 1跳转到与当前同一控制器内的action和不同控制器内的action. 2带有参数的action跳转和不带参数的action跳转. 3跳转到指定视图,不经过Contr ...
- 【转】ArcGIS API for Silverlight/WPF 2.1学习笔记(五)
2.Find示例代码 (1)xaml文件: //添加Symbol命名空间 xmlns:esriSymbols="clr-namespace:ESRI.ArcGIS.Client.Symbol ...
- English trip -- VC(情景课) 6 A Time
Words eleven 十一 twelve 十二 thirteen 十三 fourteen fifteen sixteen seventeen eighteen nineteen twenty ...
- CSS#Flex-box, border-size, onresize() event, Media Queries
Flexbox Pseudo-classes box-sizing: border-box HTML DOM event resize() @media Queries: 根据一些css条件,触发一 ...
- vue.js 过渡&动画
9-17 在add ,update, remove DOM时 提供多种方式的应用过度效果. 包括以下可选工具:(2大类,css和js) 在css过度和动画中自动应用class 配合使用第三方css动画 ...
- 12月6日 看Active Record validation ; 做jdstore ,注意gem bootstrap 版本只支持bootstrap3。
Active Record validation: new_record?()//用于验证刚新建,但没存入database中的数据 ,返回true或false persisted?() //和new_ ...
- 加密算法(DES,AES,RSA,MD5,SHA1,Base64)比较和项目应用
加密技术通常分为两大类:"对称式"和"非对称式". 对称性加密算法:对称式加密就是加密和解密使用同一个密钥.信息接收双方都需事先知道密匙和加解密算法且其密匙是相 ...
- 查询(sqlSuger)
查某一天的数据记录的条数 DateTime date1 = Convert.ToDateTime(DateTime.Now.ToShortDateString()); DateTime date2 = ...
- Python 优化第一步: 性能分析实践 使用cporfile+gprof2dot可视化
拿来主义: python -m cProfile -o profile.pstats to_profile.py gprof2dot -f pstats profile.pstats |dot -Tp ...