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 cdfac37

Browse files
feat: add typescript solution to lc problem: No.0402.Remove K Digits
1 parent 841c406 commit cdfac37

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444

4545
<!-- 这里可写通用的实现逻辑 -->
4646

47+
贪心算法
48+
4749
<!-- tabs:start -->
4850

4951
### **Python3**
@@ -62,6 +64,23 @@
6264

6365
```
6466

67+
### **TypeScript**
68+
69+
```ts
70+
function removeKdigits(num: string, k: number): string {
71+
let nums = [...num];
72+
while (k > 0) {
73+
let idx = 0;
74+
while (idx < nums.length - 1 && nums[idx + 1] >= nums[idx]) {
75+
idx++;
76+
}
77+
nums.splice(idx, 1);
78+
k--;
79+
}
80+
return nums.join('').replace(/^0*/g, '') || '0';
81+
};
82+
```
83+
6584
### **...**
6685

6786
```

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,23 @@
5757

5858
```
5959

60+
### **TypeScript**
61+
62+
```ts
63+
function removeKdigits(num: string, k: number): string {
64+
let nums = [...num];
65+
while (k > 0) {
66+
let idx = 0;
67+
while (idx < nums.length - 1 && nums[idx + 1] >= nums[idx]) {
68+
idx++;
69+
}
70+
nums.splice(idx, 1);
71+
k--;
72+
}
73+
return nums.join('').replace(/^0*/g, '') || '0';
74+
};
75+
```
76+
6077
### **...**
6178

6279
```
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function removeKdigits(num: string, k: number): string {
2+
let nums = [...num];
3+
while (k > 0) {
4+
let idx = 0;
5+
while (idx < nums.length - 1 && nums[idx + 1] >= nums[idx]) {
6+
idx++;
7+
}
8+
nums.splice(idx, 1);
9+
k--;
10+
}
11+
return nums.join('').replace(/^0*/g, '') || '0';
12+
};

0 commit comments

Comments
(0)

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