feat: 2025-10-30打卡

This commit is contained in:
wu xiangkai
2025-10-30 16:31:25 +08:00
parent b897d019fa
commit 71664a10cc
3 changed files with 135 additions and 0 deletions

38
402.remove-k-digits.java Normal file
View File

@@ -0,0 +1,38 @@
/*
* @lc app=leetcode id=402 lang=java
*
* [402] Remove K Digits
*/
// @lc code=start
class Solution {
public String removeKdigits(String num, int k) {
ArrayDeque<Character> stack = new ArrayDeque<>();
for(int i = 0; i < num.length(); i++) {
if(k == 0) {
stack.addLast(num.charAt(i));
} else {
while(k>0 && !stack.isEmpty() && stack.peekLast() > num.charAt(i)) {
k--;
stack.removeLast();
}
stack.addLast(num.charAt(i));
}
}
while(!stack.isEmpty() && k > 0) {
k--;
stack.removeLast();
}
// 消除开头的0
while(!stack.isEmpty() && stack.getFirst()=='0') {
stack.removeFirst();
}
StringBuilder sb = new StringBuilder();
for(Character c : stack) {
sb.append(c);
}
return sb.isEmpty()?"0":sb.toString();
}
}
// @lc code=end