当前位置:网站首页>LeetCode-402 - Remove K digits
LeetCode-402 - Remove K digits
2022-08-10 21:27:00 【z754916067】
题目

思路
- It feels useless to sort the numbers,Backtracking should be used for circular comparisons.
- wrote a backtrace,超出时间限制了…Backtracking is indeed time-complex,记录一下,润去题解
//targetHow many numbers to remove for the target nowHow many digits to delete for the current charfor the last deleted character For backtracking
public void Back(StringBuilder nownum,int target,int now){
if(target==now){
//比较两个字符串的大小 不能直接转 length will exceed
//赋给ans
ans = Compare(nownum.toString(),ans);
return;
}//Otherwise start subtracting
for(int i=0;i<nownum.length();i++) {
StringBuilder sb = new StringBuilder(nownum);
Back(sb.deleteCharAt(i), target, now + 1);
}
}
- Traverse the string from left to right,for each element traversed,See if you need to selectively discard the previous adjacent elements,因为对于aabbce来说,就可以判断aabbc好,还是aabbe好
- 需要注意的是,If you encounter a monotonically increasing number,Then you will always choose not to discard,But the meaning of the question must be discardedk个,为了解决这个问题,You need to let it go every time you throw it awayk减去1,That is, a discard is completed,减到0traversal can be terminated,Because enough has been lostk次了.
- 但也有可能,遍历结束后k还没有减到0,It can be reserved at this timen-kthe meaning of the numbers,So after traversal select beforen-k个即可.
代码
public String removeKdigits(String num, int k) {
int remain = num.length()-k;
//创建stack用来接收
Stack<Character> cs = new Stack<>();
for(int i=0;i<num.length();i++){
//弹出栈顶 It compares which is put on the stack
while(k!=0 && !cs.isEmpty() && cs.peek()>num.charAt(i)){
cs.pop();
k-=1;
}
cs.push(num.charAt(i));
}
//取前n-k个
StringBuilder ans = new StringBuilder();
while(!cs.isEmpty()) ans.append(cs.pop());
String ans1 = ans.reverse().substring(0,remain);
//删除前导0
int nums=0;
for(int i=0;i<ans1.length();i++){
if(ans1.charAt(i)=='0') nums++;
else break;
}
if(nums==ans1.length()) return "0";
else return ans1.substring(nums);
}
边栏推荐
- Live Classroom System 09--Tencent Cloud VOD Management Module (1)
- [Golang]用反射让你的代码变优美
- 第四届红帽杯网络安全大赛
- Bedtime story | made a Bitmap and AST length system configuration
- DDL:CREATE 创建数据库——《mysql 从入门到内卷再到入土》
- 基于Pix4Dmapper的运动结构恢复法无人机影像三维模型重建
- wget编译升级故障解决
- 直播课堂系统08补-腾讯云对象存储和课程分类管理
- Auto.js找图找色常用功能
- [Golang]如何优雅管理系统中的几十个UDF(API)
猜你喜欢

直播课堂系统08补-腾讯云对象存储和课程分类管理

姜还是老的辣,看看老战哥的老底儿和严谨劲儿
![[mysql] 深入分析MySQL版本控制MVCC规则](/img/16/e28641c355d941fda50a6e8b7911ee.png)
[mysql] 深入分析MySQL版本控制MVCC规则

Future-oriented IT infrastructure management architecture - Unified IaaS

Future与CompletableFuture

npm warn config global `--global`, `--local` are deprecated. use `--location=global` instead.

找的笔试题的复盘(一)

【vulhub】MySql身份认证绕过漏洞复现(CVE-2012-2122)

TCL:事务的特点,语法,测试例——《mysql 从入门到内卷再到入土》

LeetCode-498-对角线遍历
随机推荐
日期选择器组件(限制年份 设定仅展示的月份)
leetcode:45. 跳跃游戏II
ansible各个模块的详解和使用
第五届“强网杯”全国网络安全挑战赛(线上赛)
Common functions of Auto.js to find pictures and colors
睡前故事|用Bitmap与AST做一个配置化时长系统
kuberentes Auditing 入门
Auto.js中的悬浮窗
npm warn config global `--global`, `--local` are deprecated. use `--location=global` instead.
PostgreSQL — Installation and Common Commands
LeetCode-36-Binary search tree and doubly linked list
HGAME 2022 Week2 writeup by pankas
查询:复杂查询的语法和使用例——《mysql 从入门到内卷再到入土》
如何提交一个PR?【OpenHarmony成长计划】【OpenHarmony开源社区】
Labelme-5.0.1 version edit polygon crash
用汇编带你看Golang里到底有没有值类型、引用类型
RADIUS Authentication Server Deployment Costs That Administrators Must Know
力扣215题,数组中的第K个最大元素
CGO Preliminary Cognition and Basic Data Type Conversion
Web Reverse Lilac Garden