当前位置:网站首页>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
边栏推荐
- ASP. Net core JWT certification
- Qt error: /usr/bin/ld: cannot find -lGL: No such file or directory
- ClickHouse-表引擎
- EF core in ASP Generate core priority database based on net entity model
- Deep understanding of control inversion and dependency injection
- Learning record of uni app dark horse yougou project (Part 2)
- 1-3 components and modules
- Webapi + form form upload file
- El date picker limits the selection range from the current time to two months ago
- [registration] tf54: engineer growth map and excellent R & D organization building
猜你喜欢
JS, entries(), keys(), values(), some(), object Assign() traversal array usage
嵌入式系统中,FLASH中的程序代码必须搬到RAM中运行吗?
STM32 entry development board choose wildfire or punctual atom?
. net type transfer
C语言函数详解
Further study of data visualization
【WPF绑定3】 ListView基础绑定和数据模板绑定
1217_使用SCons生成目标文件
HCIP第五次实验
Use of five routing guards
随机推荐
Summary of common websites
Self use learning notes - connected and non connected access to database
[二叉数] 二叉树的最大深度+N叉树的最大深度
[WPF binding 3] listview basic binding and data template binding
El date picker limits the selection range from the current time to two months ago
JVM类加载机制
How does matlab draw the curve of known formula and how does excel draw the function curve image?
Change Oracle to MySQL
Router object, route object, declarative navigation, programmed navigation
Baidu Map Case - Zoom component, map scale component
1-3 components and modules
stm32入门开发板选野火还是正点原子呢?
为什么有些人说单片机简单,我学起来这么吃力?
[batch change MySQL table and corresponding codes of fields in the table]
Use of shell sed command
C listens for WMI events
Lock lock
Conversion between hexadecimal numbers
Preliminary understanding of promse
Bottom processing of stack memory in browser