Leetcode: Remove K Digits
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.
Greedy + Stack: 用一个栈维护最后要留存下来的digits
需要注意的是:如果遍历到string的某个char, string后面的char数刚刚好能填满这个栈,那么即使这个char比栈顶还要小,也不出栈,直接入栈
最后要删除leading 0
public class Solution {
public String removeKdigits(String num, int k) {
if (num.length()==0 || k>=num.length()) return "0";
char[] arr = num.toCharArray();
Stack<Character> stack = new Stack<Character>();
StringBuilder res = new StringBuilder();
int size = arr.length - k;
for (int i=0; i<arr.length; i++) {
while (!stack.isEmpty() && arr[i]<stack.peek() && (size-stack.size()+1 <= arr.length-i)) {
stack.pop();
}
if (size > stack.size())
stack.push(arr[i]);
}
while (!stack.isEmpty()) {
res.insert(0, stack.pop());
}
while (res.length()>1 && res.charAt(0)=='0') res.deleteCharAt(0);
return res.toString();
}
}
用char[]实现栈,思路一样,要快很多,beat96%
public class Solution {
public String removeKdigits(String num, int k) {
int remain = num.length() - k;
char[] numArray = num.toCharArray(), res = new char[remain];
int index = 0;
for(int i = 0; i < numArray.length; i++) {
while((numArray.length - i > remain - index) && (index > 0 && numArray[i] < res[index - 1])) index--;
if(index < remain) res[index++] = numArray[i];
}
// check leading zeroes
index = -1;
while(++index < remain) {
if(res[index] != '0') break;
}
String s = new String(res).substring(index);
return s.length() == 0 ? "0" : s;
}
}
Leetcode: 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 去掉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 解题报告(Python)
[LeetCode]402. Remove K Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- 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 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 ...
- 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 ...
- [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 ...
- Remove K Digits
Given string A representative a positive integer which has N digits, remove any k digits of the numb ...
- 【leetcode】402. Remove K Digits
题目如下: 解题思路:我的方法是从头开始遍历num,对于任意一个num[i],在[i+1~len(num)-1]区间内找出离num[i]最近并且小于num[i]的数num[j],如果j-i <= ...
随机推荐
- Laravel timestamps 设置为unix时间戳
Laravel timestamps 设置为unix时间戳 class BaseModel extends Eloquent { /** * 默认使用时间戳戳功能 * * @var bool */ p ...
- Natural Language Processing Computational Linguistics
http://www.nltk.org/book/ch00.html After this, the pace picks up, and we move on to a series of chap ...
- Least_squares 最小二乘法
https://en.wikipedia.org/wiki/Least_squares 動差估計法( MM, The Method of Moment ) 最小平方法( LSQ, The Method ...
- Intel Visual Fortran Compiler 11调用lapack库实现并行多处理计算
采用fortran进行数值计算的朋友们都应该听说过大名鼎鼎的lapack库,我就不多做介绍了,在此,我只是介绍一个编译好的lapack二进制包ACML(AMD Core Math Library),并 ...
- 地图API使用文档-以腾讯地图为例
目录 腾讯地图API 2 1.API概览... 2 1.1 WebService API(官网注明是beta版本,可能不稳定,慎用):... 2 1.2 URL API:... 2 1.3 静态图AP ...
- mark down pad2
邮箱:Soar360@live.com授权秘钥:GBPduHjWfJU1mZqcPM3BikjYKF6xKhlKIys3i1MU2eJHqWGImDHzWdD6xhMNLGVpbP2M5SN6bnxn ...
- ubuntu下的jdk安装
软件环境: 虚拟机:VMware Workstation 10 操作系统:ubuntu-12.04-desktop-amd64 JAVA版本:jdk-7u55-linux-x64 软件下载地址: JD ...
- ASP.NET MVC3更新出错:ObjectStateManager中已存在具有同一键的对象
程序代码: [HttpPost] public ActionResult Edit(Person person) { if (ModelState.IsValid) { Person oldperso ...
- 更改Magento的base url
Magento的Base URL是用于访问商店页面的URL,您也可以为单独一个store view设置一个Base Url.在改这项值之前请确保您的域名已经指向了网站所在服务器的IP,DNS解析完成后 ...
- oracle 存储过程基础
create or replace procedure update_CarryoverArchivers(bizsysname in varchar, year in number de ...