Skip to content

Commit 4280847

Browse files
authored
feat: add solutions to lc problems: No.0316,1081 (doocs#841)
* No.0316.Remove Duplicate Letters * No.1081.Smallest Subsequence of Distinct Characters
1 parent 6c9305a commit 4280847

File tree

8 files changed

+261
-18
lines changed

8 files changed

+261
-18
lines changed

solution/0300-0399/0316.Remove Duplicate Letters/README.md

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,62 @@
4040

4141
<!-- 这里可写通用的实现逻辑 -->
4242

43+
**方法一:单调栈**
44+
4345
<!-- tabs:start -->
4446

4547
### **Python3**
4648

4749
<!-- 这里可写当前语言的特殊实现逻辑 -->
4850

4951
```python
50-
52+
class Solution:
53+
def removeDuplicateLetters(self, s: str) -> str:
54+
count, in_stack = [0] * 128, [False] * 128
55+
stack = []
56+
for c in s:
57+
count[ord(c)] += 1
58+
for c in s:
59+
count[ord(c)] -= 1
60+
if in_stack[ord(c)]:
61+
continue
62+
while len(stack) and stack[-1] > c:
63+
peek = stack[-1]
64+
if count[ord(peek)] < 1:
65+
break
66+
in_stack[ord(peek)] = False
67+
stack.pop()
68+
stack.append(c)
69+
in_stack[ord(c)] = True
70+
return ''.join(stack)
5171
```
5272

53-
### **Java**
73+
### **Go**
5474

5575
<!-- 这里可写当前语言的特殊实现逻辑 -->
5676

57-
```java
58-
77+
```go
78+
func removeDuplicateLetters(s string) string {
79+
count, in_stack, stack := make([]int, 128), make([]bool, 128), make([]rune, 0)
80+
for _, c := range s {
81+
count[c] += 1
82+
}
83+
84+
for _, c := range s {
85+
count[c] -= 1
86+
if in_stack[c] {
87+
continue
88+
}
89+
for len(stack) > 0 && stack[len(stack)-1] > c && count[stack[len(stack)-1]] > 0 {
90+
peek := stack[len(stack)-1]
91+
stack = stack[0 : len(stack)-1]
92+
in_stack[peek] = false
93+
}
94+
stack = append(stack, c)
95+
in_stack[c] = true
96+
}
97+
return string(stack)
98+
}
5999
```
60100

61101
### **...**

solution/0300-0399/0316.Remove Duplicate Letters/README_EN.md

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,58 @@
3434

3535
## Solutions
3636

37+
**Monotonic Stack**
38+
3739
<!-- tabs:start -->
3840

3941
### **Python3**
4042

4143
```python
42-
44+
class Solution:
45+
def removeDuplicateLetters(self, s: str) -> str:
46+
count, in_stack = [0] * 128, [False] * 128
47+
stack = []
48+
for c in s:
49+
count[ord(c)] += 1
50+
for c in s:
51+
count[ord(c)] -= 1
52+
if in_stack[ord(c)]:
53+
continue
54+
while len(stack) and stack[-1] > c:
55+
peek = stack[-1]
56+
if count[ord(peek)] < 1:
57+
break
58+
in_stack[ord(peek)] = False
59+
stack.pop()
60+
stack.append(c)
61+
in_stack[ord(c)] = True
62+
return ''.join(stack)
4363
```
4464

45-
### **Java**
46-
47-
```java
48-
65+
### **Go**
66+
67+
```go
68+
func removeDuplicateLetters(s string) string {
69+
count, in_stack, stack := make([]int, 128), make([]bool, 128), make([]rune, 0)
70+
for _, c := range s {
71+
count[c] += 1
72+
}
73+
74+
for _, c := range s {
75+
count[c] -= 1
76+
if in_stack[c] {
77+
continue
78+
}
79+
for len(stack) > 0 && stack[len(stack)-1] > c && count[stack[len(stack)-1]] > 0 {
80+
peek := stack[len(stack)-1]
81+
stack = stack[0 : len(stack)-1]
82+
in_stack[peek] = false
83+
}
84+
stack = append(stack, c)
85+
in_stack[c] = true
86+
}
87+
return string(stack)
88+
}
4989
```
5090

5191
### **...**
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
func removeDuplicateLetters(s string) string {
2+
count, in_stack, stack := make([]int, 128), make([]bool, 128), make([]rune, 0)
3+
for _, c := range s {
4+
count[c] += 1
5+
}
6+
7+
for _, c := range s {
8+
count[c] -= 1
9+
if in_stack[c] {
10+
continue
11+
}
12+
for len(stack) > 0 && stack[len(stack)-1] > c && count[stack[len(stack)-1]] > 0 {
13+
peek := stack[len(stack)-1]
14+
stack = stack[0 : len(stack)-1]
15+
in_stack[peek] = false
16+
}
17+
stack = append(stack, c)
18+
in_stack[c] = true
19+
}
20+
return string(stack)
21+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def removeDuplicateLetters(self, s: str) -> str:
3+
count, in_stack = [0] * 128, [False] * 128
4+
stack = []
5+
for c in s:
6+
count[ord(c)] += 1
7+
for c in s:
8+
count[ord(c)] -= 1
9+
if in_stack[ord(c)]:
10+
continue
11+
while len(stack) and stack[-1] > c:
12+
peek = stack[-1]
13+
if count[ord(peek)] < 1:
14+
break
15+
in_stack[ord(peek)] = False
16+
stack.pop()
17+
stack.append(c)
18+
in_stack[ord(c)] = True
19+
return ''.join(stack)

solution/1000-1099/1081.Smallest Subsequence of Distinct Characters/README.md

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,63 @@
3838

3939
<!-- 这里可写通用的实现逻辑 -->
4040

41+
**方法一:单调栈**
42+
4143
<!-- tabs:start -->
4244

4345
### **Python3**
4446

4547
<!-- 这里可写当前语言的特殊实现逻辑 -->
4648

4749
```python
48-
50+
class Solution:
51+
def smallestSubsequence(self, s: str) -> str:
52+
count, in_stack = [0] * 128, [False] * 128
53+
stack = []
54+
for c in s:
55+
count[ord(c)] += 1
56+
57+
for c in s:
58+
count[ord(c)] -= 1
59+
if in_stack[ord(c)]:
60+
continue
61+
while len(stack) and stack[-1] > c:
62+
peek = stack[-1]
63+
if count[ord(peek)] < 1:
64+
break
65+
in_stack[ord(peek)] = False
66+
stack.pop()
67+
stack.append(c)
68+
in_stack[ord(c)] = True
69+
return ''.join(stack)
4970
```
5071

51-
### **Java**
72+
### **Go**
5273

5374
<!-- 这里可写当前语言的特殊实现逻辑 -->
5475

55-
```java
56-
76+
```go
77+
func smallestSubsequence(s string) string {
78+
count, in_stack, stack := make([]int, 128), make([]bool, 128), make([]rune, 0)
79+
for _, c := range s {
80+
count[c] += 1
81+
}
82+
83+
for _, c := range s {
84+
count[c] -= 1
85+
if in_stack[c] {
86+
continue
87+
}
88+
for len(stack) > 0 && stack[len(stack)-1] > c && count[stack[len(stack)-1]] > 0 {
89+
peek := stack[len(stack)-1]
90+
stack = stack[0 : len(stack)-1]
91+
in_stack[peek] = false
92+
}
93+
stack = append(stack, c)
94+
in_stack[c] = true
95+
}
96+
return string(stack)
97+
}
5798
```
5899

59100
### **...**

solution/1000-1099/1081.Smallest Subsequence of Distinct Characters/README_EN.md

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,59 @@
3434

3535
## Solutions
3636

37+
**Monotonic Stack**
38+
3739
<!-- tabs:start -->
3840

3941
### **Python3**
4042

4143
```python
42-
44+
class Solution:
45+
def smallestSubsequence(self, s: str) -> str:
46+
count, in_stack = [0] * 128, [False] * 128
47+
stack = []
48+
for c in s:
49+
count[ord(c)] += 1
50+
51+
for c in s:
52+
count[ord(c)] -= 1
53+
if in_stack[ord(c)]:
54+
continue
55+
while len(stack) and stack[-1] > c:
56+
peek = stack[-1]
57+
if count[ord(peek)] < 1:
58+
break
59+
in_stack[ord(peek)] = False
60+
stack.pop()
61+
stack.append(c)
62+
in_stack[ord(c)] = True
63+
return ''.join(stack)
4364
```
4465

45-
### **Java**
46-
47-
```java
48-
66+
### **Go**
67+
68+
```go
69+
func smallestSubsequence(s string) string {
70+
count, in_stack, stack := make([]int, 128), make([]bool, 128), make([]rune, 0)
71+
for _, c := range s {
72+
count[c] += 1
73+
}
74+
75+
for _, c := range s {
76+
count[c] -= 1
77+
if in_stack[c] {
78+
continue
79+
}
80+
for len(stack) > 0 && stack[len(stack)-1] > c && count[stack[len(stack)-1]] > 0 {
81+
peek := stack[len(stack)-1]
82+
stack = stack[0 : len(stack)-1]
83+
in_stack[peek] = false
84+
}
85+
stack = append(stack, c)
86+
in_stack[c] = true
87+
}
88+
return string(stack)
89+
}
4990
```
5091

5192
### **...**
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
func smallestSubsequence(s string) string {
2+
count, in_stack, stack := make([]int, 128), make([]bool, 128), make([]rune, 0)
3+
for _, c := range s {
4+
count[c] += 1
5+
}
6+
7+
for _, c := range s {
8+
count[c] -= 1
9+
if in_stack[c] {
10+
continue
11+
}
12+
for len(stack) > 0 && stack[len(stack)-1] > c && count[stack[len(stack)-1]] > 0 {
13+
peek := stack[len(stack)-1]
14+
stack = stack[0 : len(stack)-1]
15+
in_stack[peek] = false
16+
}
17+
stack = append(stack, c)
18+
in_stack[c] = true
19+
}
20+
return string(stack)
21+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def smallestSubsequence(self, s: str) -> str:
3+
count, in_stack = [0] * 128, [False] * 128
4+
stack = []
5+
for c in s:
6+
count[ord(c)] += 1
7+
8+
for c in s:
9+
count[ord(c)] -= 1
10+
if in_stack[ord(c)]:
11+
continue
12+
while len(stack) and stack[-1] > c:
13+
peek = stack[-1]
14+
if count[ord(peek)] < 1:
15+
break
16+
in_stack[ord(peek)] = False
17+
stack.pop()
18+
stack.append(c)
19+
in_stack[ord(c)] = True
20+
return ''.join(stack)

0 commit comments

Comments
 (0)