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. C#通过Oracle.ManagedDataAccess无法访问Oralce

    问题描述:通过C#引用Oracle.ManagedDataAccess.dll访问Oracle,写了如下一段代码,在本机能正常访问,但是将编译后的exe放到服务器上面就无法访问了,一直提示登录失败.而 ...

  2. Java线程池队列吃的太饱,撑着了咋整?java 队列过大导致内存溢出

    Java的Executors框架提供的定长线程池内部默认使用LinkedBlockingQueue作为任务的容器,这个队列是没有限定大小的,可以无限向里面submit任务. 当线程池处理的太慢的时候, ...

  3. 同步代码时忽略maven项目 target目录

    方式一: 在项目代码路径,如: F:\xyx\sl  鼠标右键,“TortoiseSVN”-- >“Settings” -->"Subversion"-->&qu ...

  4. 20170601xlVBA正则表达式提取体检数据

    Public Sub GetFirst() GetDataFromWord "初检" End Sub Public Sub GetDataFromWord(ByVal SheetN ...

  5. 二叉树—-1(No.9HN省赛小题)

    题目: 1013: Prototypes analyze 时间限制: 1 Sec  内存限制: 128 MB提交: 6  解决: 4[提交][状态][讨论版] 题目描述 ALpha Ceiling M ...

  6. Android studio的 repositories配置多个url

    buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2. ...

  7. JAVA计算文件的crc32校验码

    import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java ...

  8. 阿里云ECS安装最新版本Node.js

    原文  http://www.w3ctech.com/topic/1610 主题 Node.js操作系统服务器 我的ECS实例是Ubuntu操作系统,直接使用 apt-get install node ...

  9. Freemaker FTL指令常用标签及语法

    https://blog.csdn.net/pengpengpeng85/article/details/52070602 FTL指令常用标签及语法 注意:使用freemaker,要求所有标签必须闭合 ...

  10. Openwrt Support RESET Button (5)

    1 Scope of Document This document describes how to support reset button under openwrt system2 Requir ...