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

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

  2. leetcode 402. Remove K Digits

    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 、321. Create Maximum Number

    402. Remove K Digits https://www.cnblogs.com/grandyang/p/5883736.html https://blog.csdn.net/fuxuemin ...

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

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

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

  6. Leetcode: Remove K Digits

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

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

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

  9. Remove K Digits

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

  10. 【leetcode】402. Remove K Digits

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

随机推荐

  1. t-SNE 聚类

    一个有效的数据降维的方法 t-SNE,类似PCA的主成分降维分析. 参考: t-分布邻域嵌入算法(t-SNE algorithm)简单理解 t-SNE初学 很好的教程:An illustrated i ...

  2. Struts2 简介图

    Struts2官方提供的,strus2的内部工作机制图解.

  3. 49 BOM 和DOM

    一.BOM window 对象  1.确认,输入,    window.alert(123) // 弹框    let ret = window.confirm("是否删除") / ...

  4. hdu 1069 DAG加权

    题目: Monkey and Banana Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Ot ...

  5. hihoCoder-1087 Hamiltonian Cycle (记忆化搜索)

    描述 Given a directed graph containing n vertice (numbered from 1 to n) and m edges. Can you tell us h ...

  6. java.lang.string split 以点分割字符串无法正常拆分字符串

    //错误的做法String ip="192.168.11.23"; String[] spstr_IP=ip.split(".");//这种方式无法拆分在ip字 ...

  7. 未能加载文件或程序集“LinqToExcel”或它的某一个依赖项。试图加载格式不正确的程序。

    未能加载文件或程序集“*”或它的某一个依赖项.试图加载格式不正确的程序. 原因:操作系统是64位的,但发布的程序引用了一些32位的ddl,所以出现了兼容性的问题解决方案一:如果是64位机器,IIS—— ...

  8. 管道pipe与dup结合使用

    前面的例子中,子进程可以直接共享父进程的文件描述符.但是如果子进程调用exec函数执行另一个应用程序时,就不能再共享了. 这种情况下可以将子进程中的文件描述符重定向到标准输入,当新执行的程序从标准输入 ...

  9. httpclient 多线程请求

    线程请求执行 当配备一个线程池管理器后,如PollingClientConnectionManager,HttpClient就能使用执行着的多线程去执行并行的多请求. PollingClientCon ...

  10. mysql主从搭建之诡异事件

    今天在搭建主从后出现了主库system账号丢失INSERT权限的情况,记录如下 主库: system账号权限同root权限,并且mysql库已经删除 从库: mysql库存在,无system账号 主从 ...