Given string A representative a positive integer which has N digits, remove any k digits of the number, the remaining digits are arranged according to the original order to become a new positive integer.

Find the smallest integer after remove k digits.

N <= 240 and k <= N,

Example

Given an integer A = "178542", k = 4

return a string "12"

Analysis:

When take current number n from string A, we need to compare the last digit we have in the new string, if n is greater than the last number in the new string, we do nothing. However, if n is less than the last digit in the new string, should we replace it? we can if we have

newString.length() + A.length() - p > A.length() - k.

 class Solution {
public String removeKdigits(String num, int k) {
if (num == null || num.length() == || k <= ) return num;
if (num.length() <= k) return "";
StringBuilder sb = new StringBuilder();
for (int p = ; p < num.length(); p++) {
//这题关键的部分是如果当前这个数比前一个数小,我们就一定要把当前这个数替换掉前一个数。因为这样我们得到的数就一定更小。
// 但是也不是可以无限替换,我们得保证num后面部分的substring长度+当前sb的长度 >= num.length() - k
while (sb.length() >= && num.charAt(p) < sb.charAt(sb.length() - ) && sb.length() > p - k) {
sb.deleteCharAt(sb.length() - );
}
if (sb.length() < num.length() - k) {
sb.append(num.charAt(p));
}
}
// remove the extra 0 at the beginning
while(sb.length() > && sb.charAt() == '') {
sb.deleteCharAt();
}
return sb.toString();
}
}

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

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

  3. Leetcode: Remove K Digits

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

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

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

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

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

  8. 【LeetCode】402. Remove K Digits 解题报告(Python)

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

  9. 402. Remove K Digits

    (English version is after the code part) 这个题做起来比看起来容易,然后我也没仔细想,先速度刷完,以后再看有没有改进. 用这个来说: 1 2 4 3 2 2 1 ...

随机推荐

  1. BZOJ 3143 游走(贪心+期望+高斯消元)

    一个无向连通图,顶点从1编号到N,边从1编号到M. 小Z在该图上进行随机游走,初始时小Z在1号顶点,每一步小Z以相等的概率随机选 择当前顶点的某条边,沿着这条边走到下一个顶点,获得等于这条边的编号的分 ...

  2. BZOJ 1898: [Zjoi2005]Swamp 沼泽鳄鱼

    1898: [Zjoi2005]Swamp 沼泽鳄鱼 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1085  Solved: 604[Submit][S ...

  3. 美团codeM之美团代金券

    前天做了下美团的一个codeM比赛的资格赛,遇到一个题目挺有意思的,所以现在做一下总结. 题目描述 美团的每一个用户都有一个用户代金券的消费记录日志,每位用户都能购买若干种代金券,但是每一种代金券最多 ...

  4. .net mvc 一个Action的 HttpGet 和 HttpPost

    http://www.cnblogs.com/freeliver54/p/3747836.html 本文转自:http://stackoverflow.com/questions/11767911/m ...

  5. Framingham风险评估

    Framingham风险评分: Framingham 心脏研究和其他流行病学队列研究改变了20世纪后半部分对疾病的关注点,即从治疗已经存在的心血管疾病到预防处于疾病危险的状态.该策略的关键因素是识别那 ...

  6. Chapter1(预科)--C++Prime笔记

    心得体会: 因为之前一直在用在学C,因此在看完C++Prime第一章后,就有中在一个培训班中,一个老师用一个简单的项目来带你了解这种语言的特性的感觉.当然这个告诉是在让你脑子固化接受一些点的前提下. ...

  7. kubernetes 之ingress

    www.cnblogs.com/hellojackyleon/p/8420911.html apiVersion: extensions/v1beta1 kind: Deployment metada ...

  8. python pip安装--不是内部或外部命令--解决方案

    首先在 命令行中输入 python -m ensurepip 创建出pip3.exe  , 并找到pip3.exe所在的Scripts路径 使命令行在Scripts路径下 输入pip3 install ...

  9. docker中实现服务日志轮转

    问题背景 通常我们一个完整的应用镜像有两部分组成,一个是运行时环境,一个是应用程序.我们以php应用为例,一个完整的php应用需要包含openresty + php两个服务来配置运行时环境,然后再加上 ...

  10. 怎样彻底卸载(删除)SQL server2000

    如何完全卸载SQL server 2000 一.卸载安装程序:找到电脑右下角的Windows开始按钮,找到控制面板——卸载程序. 找到server 2000的安装程序,并点击后面的卸载按钮.  是,确 ...