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的更多相关文章

  1. [LeetCode] Remove K Digits 去掉K位数字

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

  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 解题报告(Python)

    [LeetCode]402. Remove K Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

  4. leetcode 402. Remove K Digits

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

  5. 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 ...

  6. 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 ...

  7. [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 ...

  8. Remove K Digits

    Given string A representative a positive integer which has N digits, remove any k digits of the numb ...

  9. 【leetcode】402. Remove K Digits

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

随机推荐

  1. mac终端中显示tree的命令

    寻觅了良久终于找到了mac下如何在终端显示tree的命令了,作为从linux下转过来的人,还没适应mac的finder,还是喜欢在命令行下查看文件. 命令: find . -print | sed - ...

  2. Java单链表的实现

    将结点Node进行封装,假设Node的操作有增加,删除,查找,打印几个操作.将Node实现为链表Link的内部类,简化代码. package Chapter5; import java.securit ...

  3. Nginx服务器

    什么是Nginx? Nginx是一种服务器软件,如同apache.tomcat.是一种高性能的HTTP和反向代理服务器以及代理邮件服务器.也就是说Nginx服务器可以发布网站,也可以负载均衡,还可以作 ...

  4. Medusa: Gauges for JavaFX

    Medusa: Gauges for JavaFX https://community.oracle.com/docs/DOC-992746

  5. C语言第一次作业

  6. 关于appstore多语言版本,不可不看!

    http://www.cocoachina.com/appstore/20160513/16256.html

  7. JVM 常用配置

    JVM的配置,最常用的两个配置就是:-Xms512m –Xmx1024m -Xms设置JVM的初始化内存大小,-Xmx为最大内存大小,当突破这个值,将会报内存溢出,导致的原因有很多,主要是虚拟机的回收 ...

  8. zepto源码--插入节点--学习笔记

    与生成width和height使用的方法类似,通过`after`, `prepend`, `before`, `append`,这四者之间的共性,生成对应的函数.并根据这四个函数,生成 `insert ...

  9. Delphi Dll示例

    //MyInt.pas unit MyInt; interface {$IFNDEF MYLIB} function MyAdd(a,b:integer):integer ;stdcall; {$EN ...

  10. HTML5新标签和属性

    1.<time>标签(支持IE9以上和其他浏览器) 今年是<time datetime="2015-12-12">2015年</time> &l ...