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 1036: [ZJOI2008]树的统计Count (树链剖分+线段树 点权)

    1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 21194  Solved: 8589[Submit ...

  2. 微信开发使用 frp 实现本地测试

    前提条件: 1.有公网服务器(如阿里云) 2.需要独立的 80 端口,也就是说,想要实现这个目标,服务器上不能跑 nginx 之类占用 80 端口的程序 3.有可以测试使用的域名,并解析到上面说的公网 ...

  3. centos禁用ipv6

    两步完成 vi /etc/sysctl.conf net.ipv6.conf.all.disable_ipv6=1sysctl -p /etc/sysctl.conf

  4. Excel 之 字符串截取、拼接、和透视表

    假设有表如下: 如何得到E列的数据(格式为模式名.表名,如PDM_DATA.T05_GMS_NAV_SPV_PCH_RDM_TRX_EVT)?如何由E列得到F列数据(从E类中截取表名)? 1. 字符串 ...

  5. np.argsort函数

    np.argsort函数 觉得有用的话,欢迎一起讨论相互学习~Follow Me numpy.argsort(a, axis=-1, kind='quicksort', order=None) 功能: ...

  6. bzoj千题计划161:bzoj1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果

    http://www.lydsy.com/JudgeOnline/problem.php?id=1589 tarjan缩环后拓扑排序上DP #include<cstdio> #includ ...

  7. 如何创建一个 Lua 模块

    如何创建一个 Lua 模块 翻译自: How to Create a Lua Module 译者: FreeBlues 正文 Lua编程语言 中的一个 模块(module)是一个包含函数和变量的代码片 ...

  8. [Luogu 3398] 仓鼠找sugar

    [Luogu 3398] 仓鼠找sugar 又是 LCA- 前两天死活写不过的一个题今天终于顺手切了. 思路嘛参考了一楼题解. 就是说,对于 a, b, c, d 四个点, 令 x = LCA(a, ...

  9. Hive性能优化--map数和reduce数

    转自http://superlxw1234.iteye.com/blog/1582880 一.    控制hive任务中的map数:  1.    通常情况下,作业会通过input的目录产生一个或者多 ...

  10. 【学习DIV+CSS】1. 你必须知道的三个知识

    1. DIV+CSS的叫法不够严谨 我们以前做页面布局的时候大多是用Table,很多人称之为“Table+CSS”,而现在比较流行的是DIV布局,所以称之为“DIV+CSS”.听起来是挺合理的,岂不知 ...