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. Everyone is tester

    有一本书叫<人人都是产品经理>,作者在书中介绍了在做产品的过程中学到的思维方式和做事方式,受到行业大众的认可 作为一名测试老鸟,我想说,其实Everyone is tester     为 ...

  2. Day24-KindEditor基本使用和文件操作1

    KindEditor是一套开源的HTML可视化编辑器,主要用于让用户在网站上获得所见即所得编辑效果,兼容IE.Firefox.Chrome.Safari.Opera等主流浏览器. 摘自老师博客:htt ...

  3. BZOJ 2938: [Poi2000]病毒

    2938: [Poi2000]病毒 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 693  Solved: 360[Submit][Status][Di ...

  4. 【bzoj2301】 HAOI2011—Problem b

    http://www.lydsy.com/JudgeOnline/problem.php?id=2301 (题目链接) 题意 给出${a,b,c,d,k}$,${n}$组询问,求$${\sum_{i= ...

  5. Luogu 1220 关路灯(动态规划)

    Luogu 1220 关路灯(动态规划) Description 某一村庄在一条路线上安装了n盏路灯,每盏灯的功率有大有小(即同一段时间内消耗的电量有多有少).老张就住在这条路中间某一路灯旁,他有一项 ...

  6. jenkins(五)---jenkins添加项目

    一.新建项目 二.配置项目 配置远程仓库:主要目的是从远程仓库拉取代码下来 实时构建 Poll SCM 定期检查 如果源码有变更 就build 否则不build build periodically ...

  7. 在eclipse中安装 Activiti Designer插件

    转: Activiti系列——如何在eclipse中安装 Activiti Designer插件 这两天在评估jbpm和Activiti,需要安装一个Activiti Designer插件试用一下. ...

  8. Docker网络 Weave

    当容器分布在多个不同的主机上时,这些容器之间的相互通信变得复杂起来.容器在不同主机之间都使用的是自己的私有IP地址,不同主机的容器之间进行通讯需要将主机的端口映射到容器的端口上,而且IP地址需要使用主 ...

  9. 利用RAP搭建可视化接口管理平台

    环境:CentOS7 jdk:1.7.0_51 redis:3.2.8 mysql:5.6 tomcat:8.0 安装过程: 依赖组件安装: 安装jdk.redis.mysql.tomcat过程省略. ...

  10. Hadoop基础原理

    Hadoop基础原理 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 业内有这么一句话说:云计算可能改变了整个传统IT产业的基础架构,而大数据处理,尤其像Hadoop组件这样的技术出 ...