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 <= ...
随机推荐
- Java 常用对象-String类
2017-11-02 20:02:06 String:代表字符串.Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现. 字符串是常量:它们的值在创建之后不能 ...
- 雷林鹏分享:C# 匿名方法
C# 匿名方法 我们已经提到过,委托是用于引用与其具有相同标签的方法.换句话说,您可以使用委托对象调用可由委托引用的方法. 匿名方法(Anonymous methods) 提供了一种传递代码块作为委托 ...
- English trip WeekEnd-Lesson 2018.11.10
本周末上了三节课,做个小结吧\(^o^)/~: [102] 新概念一早读 - 27 - 28 Teacher: March Mrs. Smith's living room is lar ...
- EBS Workflow参考资料
参考资料: How to send an email from oracle workflow process using an AdHocRole? Notification System APIs ...
- 【转载】oracle索引详解2
原文URL:http://justplayoop1.iteye.com/blog/1259562 一. 索引介绍 1.1 索引的创建 语法 : CREATE UNIUQE | BITMAP INDE ...
- 关于C++静态成员函数访问非静态成员变量的问题
静态成员函数不能访问非静态成员,这是因为静态函数属于类而不是属于整个对象,静态函数中的 member可能都没有分配内存.静态成员函数没有隐含的this自变量.所以,它就无法访问自己类的非静态成员 代码 ...
- Windows下使用Nexus搭建pypi私服
Nexus之前一直作为maven的私服而被大家所熟知,但是其实nexus可以做很多种仓库的私服,官网的说明就揭示了一切,真是又方便又强大的开源工具. 首先下载安装nexus,地址: https://w ...
- ZOJ 1985 Largest Rectangle in a Histogram(刷广告)2010辽宁省赛
Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 21204 ...
- HDU 1590 Searching(求复数向量和的极限)
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...
- 【转】重装win7后,所有USB接口无法使用(鼠标、键盘、U盘)
转自:https://blog.csdn.net/u010887744/article/details/45270245 今天给一朋友重装系统,华硕FX50J,修改BIOS重装了win7,结果所有US ...