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. FloodLight使用感受

    一个使用java语言编写的基于Openflow协议的SDN控制器. 基本架构同webserver一样,有一个维护交换机连接信息的底层模块,当有交换机同控制器连接时,floodlight会将此连接保存到 ...

  2. MySQL优化常用

    一.mysql的配置都是小写的,使用下划线_或破折号-分割单词,两者是一样的二.在配置文件中可以用1m,1g等单位,但是用set命令,不能使用单位,默认单位是字节三.特殊例子a.query_cache ...

  3. 当多个工程互相引用时,若有serverlet工程,提示java.lang.NoClassDefFoundError错误

    serverlet工程和其他的工程引用有所不同,直接在buildpath中添加引用的工程会报NoClassDefFoundError错误错误, 需要在properties-depoyment asse ...

  4. jquery用法大全

    jQuery 选择器 选择器                  实例                                   选取 *                          $ ...

  5. OpenMP for Fortran

    OpenMP for Fortran OpenMP Directive Syntax of OpenMP compiler directive for Fortran: !$OMP Directive ...

  6. 蓝牙的AVCTP协议笔记

    1.概述     AVCTP协议描述了蓝牙设备间Audio/Video的控制信号交换的格式和机制,它是一个总体的协议,具体的控制信息由其指定的协议(如AVRCP)实现,AVCTP本身只指定控制comm ...

  7. Scrum 的相关概念

    Scrum 的相关概念 4.1   Scrum 的起源 Scrum 是一种灵活的敏捷软件开发管理过程,这个名词来源于英式橄榄球.Scrum方法由Ken Schwaber和Jeff Sutherland ...

  8. 下载大图的demo by apple,值得研究和参考

    https://developer.apple.com/library/content/samplecode/LargeImageDownsizing/Introduction/Intro.html ...

  9. win7下vs2010编译生成sqlite3.dll库

    http://blog.csdn.net/qing666888/article/details/53582262 http://download.csdn.net/detail/qing666888/ ...

  10. 怎么修改tabbar的默认选中界面

        我用storyboard做了一个tabbar连接3个界面1,2,3  .程序运行默认选中加载最左边的界面1,怎么能让他默认为界面2或者3呢?菜鸟求大神知道 // 默认的selectedInde ...