当前位置:网站首页>402. 移掉 K 位数字-贪心
402. 移掉 K 位数字-贪心
2022-04-23 17:32:00 【hequnwang10】
一、题目描述
给你一个以字符串表示的非负整数 num 和一个整数 k ,移除这个数中的 k 位数字,使得剩下的数字最小。请你以字符串形式返回这个最小的数字。
示例 1:
输入:num = "1432219", k = 3
输出:"1219"
解释:移除掉三个数字 4, 3, 和 2 形成一个新的最小的数字 1219 。
示例 2:
输入:num = "10200", k = 1
输出:"200"
解释:移掉首位的 1 剩下的数字为 200. 注意输出不能有任何前导零。
示例 3:
输入:num = "10", k = 2
输出:"0"
解释:从原数字移除所有的数字,剩余为空就是 0 。
二、解题
贪心
class Solution {
public String removeKdigits(String num, int k) {
if (num == null || num.length() == 0) {
return num;
}
int length = num.length();
if (k <= 0 || k > length) {
return num;// 非法
}
if (k == length) {
return "0";
}
char[] chars = num.toCharArray();
char[] newChars = new char[length]; // 移除k个数字的结果
int newCharsTop = 0;
for (int i = 0; i < length; i++) {
while (k > 0 && newCharsTop > 0 && newChars[newCharsTop - 1] > chars[i]) {
newCharsTop--;
k--; // 移除一个数字
}
newChars[newCharsTop] = chars[i];
newCharsTop++;
}
if (k > 0) {
// 从后面移除k个数字
newCharsTop = newCharsTop - k;
k = 0;
}
// 起始位置不能是0
int startIndex = 0;
while (newChars[startIndex] == '0' && startIndex < newCharsTop) {
startIndex++;
}
// 从起始位置返回 newCharsTop - startIndex
if (newCharsTop - startIndex > 0) {
return new String(newChars, startIndex, newCharsTop- startIndex);
}
return "0";
}
}
版权声明
本文为[hequnwang10]所创,转载请带上原文链接,感谢
https://blog.csdn.net/hequnwang10/article/details/124361302
边栏推荐
- Solution architect's small bag - 5 types of architecture diagrams
- Excel quickly and automatically fills the contents of a row on a blank cell
- JS to find the character that appears three times in the string
- Wiper component encapsulation
- flink 学习(十二)Allowed Lateness和 Side Output
- 练习:求偶数和、阈值分割和求差( list 对象的两个基础小题)
- Using quartz under. Net core -- operation transfer parameters of [3] operation and trigger
- 1-3 components and modules
- Understanding and small examples of unity3d object pool
- The system cannot be started after AHCI is enabled
猜你喜欢
[PROJECT] small hat takeout (8)
[WPF binding 3] listview basic binding and data template binding
Shell script -- shell programming specification and variables
双闭环直流调速系统matlab/simulink仿真
2.Electron之HelloWorld
ASP. Net core dependency injection service life cycle
.Net Core3. 1 use razorengine NETCORE production entity generator (MVC web version)
超分之TDAN
索引:手把手教你索引从零基础到精通使用
. net cross platform principle (Part I)
随机推荐
2.Electron之HelloWorld
Abnormal resolution of Xiaomi camera
Self use learning notes - connectingstring configuration
How does matlab draw the curve of known formula and how does excel draw the function curve image?
JS failed to change all variables and changed to the return method. Finally, the problem was solved
常用SQL语句总结
C listens for WMI events
開期貨,開戶雲安全還是相信期貨公司的軟件?
Summary of common websites
Solution of Navicat connecting Oracle library is not loaded
How to sort the numbers with text in Excel from small to large instead of the first number
1-1 NodeJS
Error in v-on handler: "typeerror: cannot read property 'resetfields' of undefined"
[difference between Oracle and MySQL]
Use of shell cut command
Deep understanding of control inversion and dependency injection
ECMAScript history
Generation of barcode and QR code
Understanding of RPC core concepts
Promise (III)