题目:

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.

 题意及分析:有一个N为长的数字,用字符串来代替了,现在要求你将它删除K位,使得其得到的结果最小。对于每次删除的情况有两种

(1)假如第一个数不为0,第二个数为0,那么我们删除第一个数,就相当于数量级减少2,这样比删除得到的数其他任何一个方法都小

(2)另一种情况,我们找到第一次遍历的局部最大值,即遍历num第一个满足num.charAt(i)>num.charAt(i+1)的值,删除这个点,得到的值最小。这里就是贪心算法,每次删除一个局部最大

代码:

public class Solution {public String removeKdigits(String num, int k) {int n=0;while(true){n =num.length();if(n <= k || n == 0) return "0";if(k-- == 0){//         System.out.println(num);return num;};if(num.charAt(1)=='0'){        //第二位数为0,则删除前面一位数int firstNotZero = 1;while(firstNotZero<n&&num.charAt(firstNotZero)=='0') firstNotZero++;num=num.substring(firstNotZero);}else{      //不然寻找第一个下降的数int i=0;while(i<n-1){if(num.charAt(i)>num.charAt(i+1)){num=num.substring(0, i)+num.substring(i+1);break;}elsei++;}if(i==n-1)  num=num.substring(0, i);}}}
}

  


Seen this question in a real interview before? 

转载于:https://www.cnblogs.com/271934Liao/p/7083544.html

[LeetCode] 402. Remove K Digits Java相关推荐

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

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

  2. LeetCode Remove K Digits

    原题链接在这里:https://leetcode.com/problems/remove-k-digits/description/ 题目: Given a non-negative integer  ...

  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题库整理【Java】—— 3 无重复字符的最长子串

    LeetCode题库整理[Java] ## 3 无重复字符的最长子串 题目:给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" ...

  7. LeetCode——347. 前 K 个高频元素【最小堆实现】

    LeetCode--347. 前 K 个高频元素[最小堆实现] 给你一个整数数组 nums 和一个整数 k ,请你返回其中出现频率前 k 高的元素.你可以按 任意顺序 返回答案. 示例1: 输入: n ...

  8. LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] c++

    LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...

  9. LeetCode题库整理【Java】—— 1两数之和

    LeetCode题库整理[Java] 1.两数之和 题目:给定一个整数数组和一个目标值,找出数组中和为目标值的两个数.你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nu ...

最新文章

  1. Maven 学习Tips
  2. 【iOS-Cocos2d游戏开发之十二】浅析使用C++/C/OC进行iOS游戏混编出现“failed with exit”问题与小结;...
  3. Jenkins X:基于Kubernetes的CI/CD平台
  4. 11.8 iotop:动态显示磁盘I/O统计信息
  5. fullcalendar5.X版本 显示自定义html内容
  6. CRM WebClient UI和Hybris里工作中心跳转的url生成逻辑
  7. JavaIO流(2)--IO流原理、流的分类及节点流文件流操作
  8. 你了解SVN, CVS等版本控制器吗?
  9. 我的世界服务器修改种子,《我的世界》服务器种子代码大全 最新种子代码分享...
  10. 深度学习岗位面试记录
  11. HTML页面跳转的5种方法分析介绍
  12. springboo集成axis2实现webservice服务
  13. Aras innovator: 如何制作一个itemtype的BOM结构
  14. [BZOJ4340][BJOI2015]隐身术(后缀数组)
  15. Windows使用Hashcat
  16. 海贼王python代码_这顶海贼王的帽子,我Python给你带上了 | 【人脸识别应用】
  17. 分布式系统关注点——如何去实施「负载均衡」? 1
  18. 5G NR UE 状态转换
  19. Three.js 教程
  20. Caused by: java.lang.IllegalStateException: Failed to introspect Class [com.mchange.v2.c3p0.ComboPoo

热门文章

  1. linux sed命令替换目标字符串所在行整行为给定值
  2. Scala数组元素的修改update
  3. Python Django单表查询之日期查询(大于小于范围查询)
  4. java枚举使用示例
  5. Jenkins配置jdk(启动报错:Starting Jenkins bash: /usr/bin/java: No such file or directory)
  6. Spring Bean的属性注入
  7. 网络操作系统_全球首个大网级网络操作系统CNOS正式发布
  8. ubuntu c++ 实现自动回车键功能_从X86到ARM,实现C和C++语言90% Code自动迁移
  9. swift语言 数组定义_Swift3中数组创建方法
  10. docker迁移与备份