Remove K Digits
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,
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的更多相关文章
- [LeetCode] Remove K Digits 去掉K位数字
Given a non-negative integer num represented as a string, remove k digits from the number so that th ...
- leetcode 402. Remove K Digits
Given a non-negative integer num represented as a string, remove k digits from the number so that th ...
- Leetcode: Remove K Digits
Given a non-negative integer num represented as a string, remove k digits from the number so that th ...
- [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 ...
- 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 ...
- [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 ...
- 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 ...
- 【LeetCode】402. Remove K Digits 解题报告(Python)
[LeetCode]402. Remove K Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- 402. Remove K Digits
(English version is after the code part) 这个题做起来比看起来容易,然后我也没仔细想,先速度刷完,以后再看有没有改进. 用这个来说: 1 2 4 3 2 2 1 ...
随机推荐
- 关于SDWebImage加载高清图片导致app崩溃的问题
链接是对于SDWebImage的使用方法 http://www.cnblogs.com/JimmyBright/p/4457258.html 使用SDWebImage加载高清图片的时候,往往会报内存溢 ...
- Alpha 完结撒花 —— 事后诸葛亮
写在前面 林燊大哥 一路走来,好不容易,终于完结了. 设想和目标 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 解决的问题 用户在进店之前无法得知店铺的优劣,通过 ...
- 响应式开发(三)-----Bootstrap框架的安装使用
下载 Bootstrap 可以从http://getbootstrap.com/上下载 Bootstrap 的最新版本. Download Bootstrap:下载 Bootstrap.点击该按钮,您 ...
- Webpack 配置示例
Webpack 作为前端构建工具,对于大型网站开发,大大提升了开发效率.要使用webpack需要先安装webpack工具: 先来看一个最简单的命令 $ webpack main.js bundle.j ...
- 文档比较比对工具Beyond Compare
Beyond Compare 可以比较文件夹或文件
- 21天实战caffe笔记_第二天
1 传统机器学习 传统机器学习:通过人工设计特征提取器,将原始数据转化为合适的中间表示形式或者特征向量,利用学习系统(通常为分类器)可以对输入模式进行检测或者分类.流程如下: 传统机器学习的局限在于需 ...
- pg删除账号,权限的回收问题
在pg中删除账号时,一般不能直接删除账号,要先将该账号上所有的对应权限收回,但往往这一步是比较繁琐的,可能当时赋权的对象类型很多,对象也比较多,虽然可以通过sql按照类型来收回针对整个schema的所 ...
- C++ 注册表编程
原文 C++ 注册表编程 1.基础知识 注册表的组织方式跟文件目录比较相似,主要分为根键.子键和键值项三部分,与文件目录对应的话就是根目录.子目录和文件.分别介绍一下这三部分: (1)根键.分为5个, ...
- 转:SkipList跳表
http://kenby.iteye.com/blog/1187303 相关概念: 1.几何分布 http://baike.baidu.com/link?url=DdtNq6pCWIvr7onVBtE ...
- Spring boot 集成 mybaits plus(代码生成器)
源码地址:https://github.com/YANGKANG01/Spring-Boot-Demo 代码生成操作 在pom.xml文件中引入以下包: <!-- mybatisplus与spr ...