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. java--接口和抽象类

    接口将抽象类的概念更延伸了一步,完全禁止了所有的函数定义.且可以将多个接口合并到一起,但是不能继承多个类.

  2. mysql从只有一个备份文件(多个数据库的备份)中恢复数据到指定数据库

    mysql -uroot -p 要恢复的数据库的名字 --one-database<备份文件

  3. laravel md5+salt 密码

    laravel 默认用的登录密码加密方式是: $password = Hash::make('password'); 修改密码加密方式为: $password = md5('password'.'sa ...

  4. Java内存管理和垃圾回收

    笔记,深入理解java虚拟机 Java运行时内存区域 程序计数器,线程独占,当前线程所执行的字节码的行号指示器,每个线程需要记录下执行到哪儿了,下次调度的时候可以继续执行,这个区是唯一不会发生oom的 ...

  5. Machine Learning in Action – PCA和SVD

    降维技术, 首先举的例子觉得很好,因为不知不觉中天天都在做着降维的工作 对于显示器显示一个图片是通过像素点0,1,比如对于分辨率1024×768的显示器,就需要1024×768个像素点的0,1来表示, ...

  6. ExtJS 刷新或者重载Tree后,默认选中刷新前最后一次选中的节点代码片段

    //tree对象 var tree = Main.getPageControler().treePanel; //获取选中的节点 var node = tree.getSelectionModel() ...

  7. mssql 常用SQL语句或函数

    按 OrderDate 的顺序计算 SalesOrderHeader 表中所有行的行号,并只返回行 50 到 60(含). WITH OrderedOrders AS ( SELECT SalesOr ...

  8. ArcGIS API for JavaScript 4.0(一)

    原文:ArcGIS API for JavaScript 4.0(一) 最近ArcGIS推出了ArcGIS API for JavaScript 4.0,支持无插件3D显示,而且比较Unity和Sky ...

  9. LightOj 1098 - A New Function(求1-n所有数的因子和)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1098 题意:给你一个数n (0 ≤ n ≤ 2 * 109),求n以内所有数的因子和, ...

  10. mybatis的xlm的sql

    <sqlMap namespace="egis.scms.order">    <typeAlias alias="ScmsOrderDTO" ...