Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 09fb169

Browse files
committed
add day-13
1 parent 0414d58 commit 09fb169

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@
1313
- Day 10: [Find Town Judge](https://github.com/libterty/leetcode-challenge/blob/master/src/may/day-ten/index.ts) :poop:
1414
- Day 11: [Flood Fill](https://github.com/libterty/leetcode-challenge/blob/master/src/may/day-eleven/index.ts) :raising_hand:
1515
- Day 12: [Single Element in a Sorted Array](https://github.com/libterty/leetcode-challenge/blob/master/src/may/day-twelve/index.ts) :hear_no_evil:
16+
- Day 13: [Remove K Digits](https://github.com/libterty/leetcode-challenge/blob/master/src/may/day-thirteen/index.ts) :tongue:

src/may/day-thirteen/index.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @param {string} num
3+
* @param {number} k
4+
* @return {string}
5+
*/
6+
export const removeKdigits = function (num: string, k) {
7+
if (num.length === k) return '0';
8+
// 堆疊解法
9+
let i: number = 0;
10+
let stack: string[] = [];
11+
while (i < num.length) {
12+
while (k > 0 && stack.length && stack[stack.length - 1] > num[i]) {
13+
stack.pop();
14+
k--;
15+
}
16+
stack.push(num[i]);
17+
i++;
18+
}
19+
stack = k > 0 ? stack.slice(0, -k) : stack;
20+
return stack.join('').replace(/^0+/, '') || '0';
21+
};
22+
23+
const a = removeKdigits('0354', 1);
24+
const b = removeKdigits('10200', 1);
25+
const c = removeKdigits('112', 1);
26+
console.log('a', a);
27+
console.log('b', b);
28+
console.log('c', c);

0 commit comments

Comments
 (0)