Skip to content

Commit 3aed6f1

Browse files
committed
feat: add solutions to lc problem: No.0942
No.0942.DI String Match
1 parent 4cff39a commit 3aed6f1

File tree

6 files changed

+209
-2
lines changed

6 files changed

+209
-2
lines changed

solution/0900-0999/0942.DI String Match/README.md

+74-1
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,95 @@
4747

4848
<!-- 这里可写通用的实现逻辑 -->
4949

50+
类似贪心思想,如果当前字母是 `I`,我们只需要选择当前可选的最小数字,就能保证后面的数字无论怎么排列,当前数字和下一个数字一定是递增关系。`D` 同理,选择当前可选的最大数字即可
51+
5052
<!-- tabs:start -->
5153

5254
### **Python3**
5355

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

5658
```python
57-
59+
class Solution:
60+
def diStringMatch(self, s: str) -> List[int]:
61+
n = len(s)
62+
low, high = 0, n
63+
ans = []
64+
for i in range(n):
65+
if s[i] == 'I':
66+
ans.append(low)
67+
low += 1
68+
else:
69+
ans.append(high)
70+
high -= 1
71+
ans.append(low)
72+
return ans
5873
```
5974

6075
### **Java**
6176

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

6479
```java
80+
class Solution {
81+
public int[] diStringMatch(String s) {
82+
int n = s.length();
83+
int low = 0, high = n;
84+
int[] ans = new int[n + 1];
85+
for (int i = 0; i < n; i++) {
86+
if (s.charAt(i) == 'I') {
87+
ans[i] = low++;
88+
} else {
89+
ans[i] = high--;
90+
}
91+
}
92+
ans[n] = low;
93+
return ans;
94+
}
95+
}
96+
```
97+
98+
### **C++**
99+
100+
```cpp
101+
class Solution {
102+
public:
103+
vector<int> diStringMatch(string s) {
104+
int n = s.size();
105+
int low = 0, high = n;
106+
vector<int> ans(n + 1);
107+
for (int i = 0; i < n; ++i) {
108+
if (s[i] == 'I') {
109+
ans[i] = low++;
110+
} else {
111+
ans[i] = high--;
112+
}
113+
}
114+
ans[n] = low;
115+
return ans;
116+
}
117+
};
118+
```
65119
120+
### **Go**
121+
122+
```go
123+
func diStringMatch(s string) []int {
124+
n := len(s)
125+
low, high := 0, n
126+
var ans []int
127+
for i := 0; i < n; i++ {
128+
if s[i] == 'I' {
129+
ans = append(ans, low)
130+
low++
131+
} else {
132+
ans = append(ans, high)
133+
high--
134+
}
135+
}
136+
ans = append(ans, low)
137+
return ans
138+
}
66139
```
67140

68141
### **...**

solution/0900-0999/0942.DI String Match/README_EN.md

+72-1
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,84 @@
3939
### **Python3**
4040

4141
```python
42-
42+
class Solution:
43+
def diStringMatch(self, s: str) -> List[int]:
44+
n = len(s)
45+
low, high = 0, n
46+
ans = []
47+
for i in range(n):
48+
if s[i] == 'I':
49+
ans.append(low)
50+
low += 1
51+
else:
52+
ans.append(high)
53+
high -= 1
54+
ans.append(low)
55+
return ans
4356
```
4457

4558
### **Java**
4659

4760
```java
61+
class Solution {
62+
public int[] diStringMatch(String s) {
63+
int n = s.length();
64+
int low = 0, high = n;
65+
int[] ans = new int[n + 1];
66+
for (int i = 0; i < n; i++) {
67+
if (s.charAt(i) == 'I') {
68+
ans[i] = low++;
69+
} else {
70+
ans[i] = high--;
71+
}
72+
}
73+
ans[n] = low;
74+
return ans;
75+
}
76+
}
77+
```
78+
79+
### **C++**
80+
81+
```cpp
82+
class Solution {
83+
public:
84+
vector<int> diStringMatch(string s) {
85+
int n = s.size();
86+
int low = 0, high = n;
87+
vector<int> ans(n + 1);
88+
for (int i = 0; i < n; ++i) {
89+
if (s[i] == 'I') {
90+
ans[i] = low++;
91+
} else {
92+
ans[i] = high--;
93+
}
94+
}
95+
ans[n] = low;
96+
return ans;
97+
}
98+
};
99+
```
48100
101+
### **Go**
102+
103+
```go
104+
func diStringMatch(s string) []int {
105+
n := len(s)
106+
low, high := 0, n
107+
var ans []int
108+
for i := 0; i < n; i++ {
109+
if s[i] == 'I' {
110+
ans = append(ans, low)
111+
low++
112+
} else {
113+
ans = append(ans, high)
114+
high--
115+
}
116+
}
117+
ans = append(ans, low)
118+
return ans
119+
}
49120
```
50121

51122
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
vector<int> diStringMatch(string s) {
4+
int n = s.size();
5+
int low = 0, high = n;
6+
vector<int> ans(n + 1);
7+
for (int i = 0; i < n; ++i) {
8+
if (s[i] == 'I') {
9+
ans[i] = low++;
10+
} else {
11+
ans[i] = high--;
12+
}
13+
}
14+
ans[n] = low;
15+
return ans;
16+
}
17+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
func diStringMatch(s string) []int {
2+
n := len(s)
3+
low, high := 0, n
4+
var ans []int
5+
for i := 0; i < n; i++ {
6+
if s[i] == 'I' {
7+
ans = append(ans, low)
8+
low++
9+
} else {
10+
ans = append(ans, high)
11+
high--
12+
}
13+
}
14+
ans = append(ans, low)
15+
return ans
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int[] diStringMatch(String s) {
3+
int n = s.length();
4+
int low = 0, high = n;
5+
int[] ans = new int[n + 1];
6+
for (int i = 0; i < n; i++) {
7+
if (s.charAt(i) == 'I') {
8+
ans[i] = low++;
9+
} else {
10+
ans[i] = high--;
11+
}
12+
}
13+
ans[n] = low;
14+
return ans;
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def diStringMatch(self, s: str) -> List[int]:
3+
n = len(s)
4+
low, high = 0, n
5+
ans = []
6+
for i in range(n):
7+
if s[i] == 'I':
8+
ans.append(low)
9+
low += 1
10+
else:
11+
ans.append(high)
12+
high -= 1
13+
ans.append(low)
14+
return ans

0 commit comments

Comments
 (0)