Skip to content

Commit cdfac37

Browse files
committedAug 20, 2021
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

+19
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

+17
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
```
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)
Please sign in to comment.