Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 50c804c

Browse files
authored
feat: update ts solution to lc problem: No.0402 (doocs#2567)
1 parent a4f0b14 commit 50c804c

File tree

3 files changed

+30
-24
lines changed

3 files changed

+30
-24
lines changed

‎solution/0400-0499/0402.Remove K Digits/README.md‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -145,16 +145,18 @@ func removeKdigits(num string, k int) string {
145145

146146
```ts
147147
function removeKdigits(num: string, k: number): string {
148-
let nums = [...num];
149-
while (k>0) {
150-
let idx =0;
151-
while (idx<nums.length-1&&nums[idx+1] >=nums[idx]) {
152-
idx++;
148+
const stk:string[] = [];
149+
for (const c ofnum) {
150+
while (k&&stk.length>0&&stk[stk.length-1] >c) {
151+
stk.pop();
152+
k--;
153153
}
154-
nums.splice(idx, 1);
155-
k--;
154+
stk.push(c);
156155
}
157-
return nums.join('').replace(/^0*/g, '') || '0';
156+
while (k--) {
157+
stk.pop();
158+
}
159+
return stk.join('').replace(/^0*/g, '') || '0';
158160
}
159161
```
160162

‎solution/0400-0499/0402.Remove K Digits/README_EN.md‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,16 +131,18 @@ func removeKdigits(num string, k int) string {
131131

132132
```ts
133133
function removeKdigits(num: string, k: number): string {
134-
let nums = [...num];
135-
while (k>0) {
136-
let idx =0;
137-
while (idx<nums.length-1&&nums[idx+1] >=nums[idx]) {
138-
idx++;
134+
const stk:string[] = [];
135+
for (const c ofnum) {
136+
while (k&&stk.length>0&&stk[stk.length-1] >c) {
137+
stk.pop();
138+
k--;
139139
}
140-
nums.splice(idx, 1);
141-
k--;
140+
stk.push(c);
142141
}
143-
return nums.join('').replace(/^0*/g, '') || '0';
142+
while (k--) {
143+
stk.pop();
144+
}
145+
return stk.join('').replace(/^0*/g, '') || '0';
144146
}
145147
```
146148

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
function removeKdigits(num: string, k: number): string {
2-
letnums= [...num];
3-
while(k>0) {
4-
letidx=0;
5-
while(idx<nums.length-1&&nums[idx+1]>=nums[idx]){
6-
idx++;
2+
conststk: string[]= [];
3+
for(constcofnum) {
4+
while(k&&stk.length>0&&stk[stk.length-1]>c){
5+
stk.pop();
6+
k--;
77
}
8-
nums.splice(idx, 1);
9-
k--;
8+
stk.push(c);
109
}
11-
return nums.join('').replace(/^0*/g, '') || '0';
10+
while (k--) {
11+
stk.pop();
12+
}
13+
return stk.join('').replace(/^0*/g, '') || '0';
1214
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /