原题链接在这里:https://leetcode.com/problems/remove-k-digits/description/

题目:

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.

题解:

利用stack 保存从头到尾iterate input num string的char, 若当前char 比stack顶小就一直pop栈顶直到 去掉的数等于k了或者栈顶的元素更小.

然后从头到尾找到第一个非0的位置 往后扫剩余digit长度的char.

扫过的0也应该count在剩余digit长度中,只不过不会显示在结果string里.

Time Complexity: O(n). n = num.length().

Space: O(n).

AC Java:

 1 class Solution {
 2     public String removeKdigits(String num, int k) {
 3         if(num == null || num.length() == 0){
 4             return num;
 5         }
 6
 7         int len = num.length();
 8         int remainDigits = len-k;
 9         char [] stk = new char[len];
10         int top = 0;
11         for(int i = 0; i<len; i++){
12             char c = num.charAt(i);
13             while(top>0 && c<stk[top-1] && k>0){
14                 top--;
15                 k--;
16             }
17
18             stk[top++] = c;
19         }
20
21         // 找到第一个不为0的index
22         int ind = 0;
23         while(ind<remainDigits && stk[ind]=='0'){
24             ind++;
25         }
26         return ind == remainDigits ? "0" : new String(stk, ind, remainDigits-ind);
27     }
28 }

类似Create Maximum Number.

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/8327380.html

LeetCode Remove K Digits相关推荐

  1. [LeetCode] 402. Remove K Digits Java

    题目: Given a non-negative integer num represented as a string, remove k digits from the number so tha ...

  2. leetcode 402. Remove K Digits | 402. 移掉 K 位数字(单调栈)

    题目 https://leetcode.com/problems/remove-k-digits/ 题解 本题考察对问题的抽象能力,多写几个例子可以发现,这是一个单调栈问题,维护一个单调不减栈. cl ...

  3. leetcode算法题--Remove K Digits

    原题链接:https://leetcode.com/problems/remove-k-digits/ string removeKdigits(string num, int k) {string ...

  4. 贪心:remove K digits移除K个数字

    问题描述: 已知一个使用字符串表示的非负整数num,将num中的k个数字移 除,求移除k个数字后,可以获得的最小的可能的新数字. 例如:num = "1432219" , k = ...

  5. LeetCode-Remove K Digits

    题目 Remove K Digits Given a non-negative integer num represented as a string, remove k digits from th ...

  6. LeetCode:Remove Duplicates from Sorted List I II

    LeetCode:Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such t ...

  7. LeetCode 787. K 站中转内最便宜的航班(图/Bellman Ford算法)

    文章目录 贝尔曼-福特算法(Bellman-Ford) 简介 算法思想 算法执行过程 应用 题目描述 分析 代码 LeetCode 787. K 站中转内最便宜的航班 题目描述 Bellman For ...

  8. LeetCode——787. K 站中转内最便宜的航班(Cheapest Flights Within K Stops)[中等]——分析及代码(Java)

    LeetCode--787. K 站中转内最便宜的航班[Cheapest Flights Within K Stops][中等]--分析及代码[Java] 一.题目 二.分析及代码 1. 动态规划 ( ...

  9. LeetCode 787.K站中转内最便宜的航班

    LeetCode 787.K站中转内最便宜的航班 有 n 个城市通过 m 个航班连接.每个航班都从城市 u 开始,以价格 w 抵达 v. 现在给定所有的城市和航班,以及出发城市 src 和目的地 ds ...

最新文章

  1. HTML5 特性检测:Canvas(画布)
  2. OpenJudge计算概论-找和为K的两个元素
  3. PAT甲级1068 Find More Coins (30 分):[C++题解]DP、背包问题、dp输出方案
  4. Spring5的集成测试
  5. Node中使用token(基于第三方包jsonwebtoken)
  6. tomcat给android发图片,一步一步学会http获取tomcat服务端的图片,在android客户端显示...
  7. 华中邀请赛现场赛F题 Seats
  8. 大数据之-Hadoop3.x_MapReduce_序列化案例Debug调试---大数据之hadoop3.x工作笔记0101
  9. Linux中inode值是什么?
  10. BZOJ4197: [Noi2015]寿司晚宴
  11. 如何下载行政区划数据
  12. 积分简明笔记-第一类曲线积分的类型
  13. 百度网盘资源转迅雷下载正确打开方式!
  14. python对象与json字符串的相互转化
  15. E/MicroMsg.SDK.WXMediaMessage(17582): checkArgs fail, thumbData is invalid
  16. 使用containerd管理容器【同docker】【或称之为docker替代品】
  17. 【python基础】英文大小写函数
  18. IC Compiler 实验三
  19. java面试题大全2
  20. 机器学习--模型参数优化及scoring可选参数

热门文章

  1. css就近原则_CSS的引入方式和优先级
  2. shiro-cas------整合springboot客户端
  3. 如何实现降维处理(R语言)
  4. php query builder,php – Symfony2 – Doctrine2 QueryBuilder WHERE I...
  5. 图像形状特征(六)--AR模型形状描述子
  6. wap建站程序源码_角点科技:企业搭建网站选择建站公司需要注意什么?
  7. php实现排序,PHP实现各种排序
  8. 海尔智能微型计算机,微型计算机 Microcomputers
  9. TCP模块如何处理数据包
  10. 堆的应用之优先级队列