(English version is after the code part)

这个题做起来比看起来容易,然后我也没仔细想,先速度刷完,以后再看有没有改进。

用这个来说:

1 2 4 3 2 2 1 9

去掉1位的话,应该去掉4,得到 1 2 3 2 2 1 9

去掉2位的话,在刚才的基础上,去掉3,得到1 2 2 2 1 9.

显而易见,每次找第一个最大值。上面的例子,第一次找到的是4,第二次是3,下一个数开始变小就不找了,所以找不到最后的9.

然后就各种特殊情况。

首先例子已经给了一个提示: "10200", k = 1

第一位之后如果是0,就要去掉第一位和后面所有的0)。

还有特殊情况,比如相等: "122211" "122231"

前者在相等之后,出现一个小的 1,所以去掉相等的其中一个。

后者在相等之后,出现一个大的 3,所以去掉3.

可以在出现等于的时候就记住第一个相等元素的index,以便最后出现一个小的,直接去掉INDEX的元素就行了。

也可以不用记,因为最终搜索停止前者停在最后1个2,后者停在3,正好是要去掉的元素。但是这里牵扯一个问题,就是搜索到底,12222或者12345这样的情况,要去掉最后一个。

剩下的就是一步能判断的情况,比如k = num.length, 最后是空字符就返还0之类的。

代码就按部就班写的,AC之后再没管,过两天数据多了再改进,求不喷。

public class Solution {
public String removeKdigits(String num, int k)
{
if(k == 0 || num.length() == 0) return num;
if(k == num.length()) return "0"; for(int i = 0; i < k;i++)
{
int j = 0;
if(j+1 < num.length() && num.charAt(j+1) == '0') num = num.substring(2);
else
{
boolean finish = false;
j = 0;
while(j+1 < num.length())
{
if(num.charAt(j) <= num.charAt(j+1)) j++;
else
{
finish = true;
num = num.substring(0,j) + num.substring(j+1);
break;
}
} if(!finish) num = num.substring(0,num.length()-1);
} int z = 0;
while(z < num.length() && num.charAt(z) == '0') z++;
num = num.substring(z); }
if(num.length() == 0) return "0";
return num;
} }

See this eg below:

1 2 4 3 2 2 1 9

When k = 1, meaning get rid of 1 digit, then we shall remove element 4, and get a result

1 2 3 2 2 1 9

When k = 2,based on previous step, we remove 3, and get

1 2 2 2 1 9

The rule is every time we wanna remove an element, we search from index 0, find the first local max value. And that's it.

The rest are just edge cases. Examples in description alredy provided 2 for us.

When the first digit is following by several 0s, we shall remove the first digit and all the following 0s.

num.length == k, return "0"

When searching for a local max value, we move on if 2 neighbor elements are the same, and decide later.

If an empty string left, return "0".

Just an acceptable version.

402. Remove K Digits的更多相关文章

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

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

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

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

  4. leetcode 402. 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. 402 Remove K Digits 移掉K位数字

    给定一个以字符串表示的非负整数 num,移除这个数中的 k 位数字,使得剩下的数字最小.注意:    num 的长度小于 10002 且 ≥ k.    num 不会包含任何前导零.示例 1 :输入: ...

  7. 【leetcode】402. Remove K Digits

    题目如下: 解题思路:我的方法是从头开始遍历num,对于任意一个num[i],在[i+1~len(num)-1]区间内找出离num[i]最近并且小于num[i]的数num[j],如果j-i <= ...

  8. [LeetCode] Remove K Digits 去掉K位数字

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

  9. Leetcode: Remove K Digits

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

随机推荐

  1. struts2 测试错题解析

    解析:$.parseJSON()方法是将字符串转换成Json类型数据,$.getJSON()方法是获取JSON数据,两者不用联合使用. 解析: A:ActionContext接口没有getReques ...

  2. a-b(高精度)

    我现在已经是才语言中的一员了,我在此献上今日的佳作——a-b(高精度),以下是我的程序及其注释,欢迎各位来观赏,耶! 程序: #include<stdio.h> #include<s ...

  3. ACM YTU 十进制与八进制的转换 (栈和队列) STL栈调用

    十进制与八进制的转换(栈和队列) Description 对于输入的任意一个非负十进制整数,利用栈打印输出与其等值的八进制数. Input 111 Output 157 Sample Input 14 ...

  4. 《wc》-linux命令五分钟系列之十七

    本原创文章属于<Linux大棚>博客,博客地址为http://roclinux.cn.文章作者为rocrocket. 为了防止某些网站的恶性转载,特在每篇文章前加入此信息,还望读者体谅. ...

  5. 使用JsPlumb绘制拓扑图的通用方法

    转自:http://www.it165.net/pro/html/201311/7616.html 使用JsPlumb绘制拓扑图的通用方法 一. 实现目标 绘制拓扑图, 实际上是个数据结构和算法的问题 ...

  6. ES6-个人学习笔记二--解构赋值

    第二期,解构赋值如果能够熟练应用确实是个十分方便的功能,但是过分的依赖和嵌套只会让代码理解和维护起来十分困难,是个体现高逼格的表达式呢~ 1,解构赋值的基础 //定义:es6运行按照一定模式,从数组或 ...

  7. 从Image Caption Generation理解深度学习

    0. 前面的话 建丁让我写一篇深度学习相关小文章,目标读者是国内的开发者.刚接到这个任务时我是颇为忐忑的,写文章要讲究厚积薄发,如果“水之积也不厚”,“则其负大舟也无力”.因为我自知水平很有限,又不是 ...

  8. WAMP(Windows+Apache+Mysql+PHP)环境搭建

    学习PHP已经有一段时间,一直没有写过关于开发环境搭建的笔记,现在补上吧,因为安装配置的步骤记得不是很清楚,借鉴了一些别人的经验,总结如下: 首先去官方网站下载各个软件,下载需要的版本: Apache ...

  9. php模块参考

    <?php //数据库连接类 class ConnDB{ var $dbtype; var $host; var $user; var $pwd; var $dbname; //构造方法 fun ...

  10. 在Docker下部署Nginx

    在Docker下部署Nginx 在Docker下部署Nginx,包括: 部署一个最简单的Nginx,可以通过80端口访问默认的网站 设置记录访问和错误日志的路径 设置静态网站的路径 通过proxy_p ...