Leetcode: 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.
Greedy + Stack: 用一个栈维护最后要留存下来的digits
需要注意的是:如果遍历到string的某个char, string后面的char数刚刚好能填满这个栈,那么即使这个char比栈顶还要小,也不出栈,直接入栈
最后要删除leading 0
public class Solution {
public String removeKdigits(String num, int k) {
if (num.length()==0 || k>=num.length()) return "0";
char[] arr = num.toCharArray();
Stack<Character> stack = new Stack<Character>();
StringBuilder res = new StringBuilder();
int size = arr.length - k;
for (int i=0; i<arr.length; i++) {
while (!stack.isEmpty() && arr[i]<stack.peek() && (size-stack.size()+1 <= arr.length-i)) {
stack.pop();
}
if (size > stack.size())
stack.push(arr[i]);
}
while (!stack.isEmpty()) {
res.insert(0, stack.pop());
}
while (res.length()>1 && res.charAt(0)=='0') res.deleteCharAt(0);
return res.toString();
}
}
用char[]实现栈,思路一样,要快很多,beat96%
public class Solution {
public String removeKdigits(String num, int k) {
int remain = num.length() - k;
char[] numArray = num.toCharArray(), res = new char[remain];
int index = 0;
for(int i = 0; i < numArray.length; i++) {
while((numArray.length - i > remain - index) && (index > 0 && numArray[i] < res[index - 1])) index--;
if(index < remain) res[index++] = numArray[i];
} // check leading zeroes
index = -1;
while(++index < remain) {
if(res[index] != '0') break;
}
String s = new String(res).substring(index); return s.length() == 0 ? "0" : s;
}
}
Leetcode: Remove K Digits的更多相关文章
- [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 去掉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 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 ...
- 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 <= ...
随机推荐
- ip sevices
http://www.ip138.com/ip2city.asp http://www.bliao.com/ip.phtml http://www.whereismyip.com/ http://ww ...
- 11号了,还有三天上线-改bug
+(NSDictionary *)replacedKeyFromPropertyName { return @{ @"doctorId": @"id" }; ...
- 线性表集合A=A B
大话数据结构 void union(List *a, List Lb) { int La_len, Lb_len, i; ElemType e; La_len = ListLength(La); Lb ...
- 纯css下拉菜单的制作
通过观察下拉菜单的过程,发现有两种状态,一种是鼠标没有hover时,一种是鼠标hover时. 主菜单hover时,显示子菜单:主菜单没有hover时,不显示子菜单 <!DOCTYPE html& ...
- WAMP数据库环境搭建
php.ini: date.timezone = Etc/GMT-8//设置北京时间 my.ini: character_set_server=utf8//设置utf8 innodb_force_re ...
- 【总结】使用jdbc+servlet开发一个bug管理系统的经验总结
开发背景: 公司目前使用Teambition里面的task作为bug管理系统,既没有bug的当前状态,也不能写上bug的详细复现步骤,被assign了任务(该修复bug或者验证bug是否被修复)也没有 ...
- settimeout 传递带有参数的函数
方法一:传递带有参数的function给settimeout,写个函数,该函数返回一个不带参数的函数 <script language="javascript"> fu ...
- 浅析在QtWidget中自定义Model
Qt 4推出了一组新的item view类,它们使用model/view结构来管理数据与表示层的关系.这种结构带来的功能上的分离给了开发人员更大的弹性来定制数据项的表示,它也提供一个标准的model接 ...
- Swift-04-Designated&&Convenience
class ClassA { let numA:Int init(num: Int){ numA = num } } class ClassB: ClassA { let numB:Int overr ...
- SVN Working Copy locked ,并且进行clean up也还是不行
标题:working copy locked 提示:your working copy appears to be locked. run cleanup to amend the situation ...