Skip to content

Commit a6ef630

Browse files
committed
feat: add solutions to lc problems
* No.0028.Find the Index of the First Occurrence in a String * No.0484.Find Permutation
1 parent ef4e652 commit a6ef630

File tree

23 files changed

+839
-334
lines changed

23 files changed

+839
-334
lines changed

solution/0000-0099/0028.Implement strStr()/README.md solution/0000-0099/0028.Find the Index of the First Occurrence in a String/README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# [28. 实现 strStr()](https://leetcode.cn/problems/implement-strstr)
1+
# [28. 实现 strStr()](https://leetcode.cn/problems/find-the-index-of-the-first-occurrence-in-a-string)
22

3-
[English Version](/solution/0000-0099/0028.Implement%20strStr%28%29/README_EN.md)
3+
[English Version](/solution/0000-0099/0028.Find%20the%20Index%20of%20the%20First%20Occurrence%20in%20a%20String/README_EN.md)
44

55
## 题目描述
66

@@ -41,6 +41,7 @@
4141
<li><code>haystack</code> 和 <code>needle</code> 仅由小写英文字符组成</li>
4242
</ul>
4343

44+
4445
## 解法
4546

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

solution/0000-0099/0028.Implement strStr()/README_EN.md solution/0000-0099/0028.Find the Index of the First Occurrence in a String/README_EN.md

+11-15
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,27 @@
1-
# [28. Implement strStr()](https://leetcode.com/problems/implement-strstr)
1+
# [28. Find the Index of the First Occurrence in a String](https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string)
22

3-
[中文文档](/solution/0000-0099/0028.Implement%20strStr%28%29/README.md)
3+
[中文文档](/solution/0000-0099/0028.Find%20the%20Index%20of%20the%20First%20Occurrence%20in%20a%20String/README.md)
44

55
## Description
66

7-
<p>Implement <a href="http://www.cplusplus.com/reference/cstring/strstr/" target="_blank">strStr()</a>.</p>
8-
97
<p>Given two strings <code>needle</code> and <code>haystack</code>, return the index of the first occurrence of <code>needle</code> in <code>haystack</code>, or <code>-1</code> if <code>needle</code> is not part of <code>haystack</code>.</p>
108

11-
<p><strong>Clarification:</strong></p>
12-
13-
<p>What should we return when <code>needle</code> is an empty string? This is a great question to ask during an interview.</p>
14-
15-
<p>For the purpose of this problem, we will return 0 when <code>needle</code> is an empty string. This is consistent to C&#39;s <a href="http://www.cplusplus.com/reference/cstring/strstr/" target="_blank">strstr()</a> and Java&#39;s <a href="https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(java.lang.String)" target="_blank">indexOf()</a>.</p>
16-
179
<p>&nbsp;</p>
18-
<p><strong>Example 1:</strong></p>
10+
<p><strong class="example">Example 1:</strong></p>
1911

2012
<pre>
21-
<strong>Input:</strong> haystack = &quot;hello&quot;, needle = &quot;ll&quot;
22-
<strong>Output:</strong> 2
13+
<strong>Input:</strong> haystack = &quot;sadbutsad&quot;, needle = &quot;sad&quot;
14+
<strong>Output:</strong> 0
15+
<strong>Explanation:</strong> &quot;sad&quot; occurs at index 0 and 6.
16+
The first occurrence is at index 0, so we return 0.
2317
</pre>
2418

25-
<p><strong>Example 2:</strong></p>
19+
<p><strong class="example">Example 2:</strong></p>
2620

2721
<pre>
28-
<strong>Input:</strong> haystack = &quot;aaaaa&quot;, needle = &quot;bba&quot;
22+
<strong>Input:</strong> haystack = &quot;leetcode&quot;, needle = &quot;leeto&quot;
2923
<strong>Output:</strong> -1
24+
<strong>Explanation:</strong> &quot;leeto&quot; did not occur in &quot;leetcode&quot;, so we return -1.
3025
</pre>
3126

3227
<p>&nbsp;</p>
@@ -37,6 +32,7 @@
3732
<li><code>haystack</code> and <code>needle</code> consist of only lowercase English characters.</li>
3833
</ul>
3934

35+
4036
## Solutions
4137

4238
<!-- tabs:start -->

solution/0400-0499/0484.Find Permutation/README.md

+105-1
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,126 @@
4646

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

49+
**方法一:贪心**
50+
51+
先初始化结果数组 `ans``[1, 2, 3, ..., n+1]`
52+
53+
假定某个连续 `D` 子数组区间为 `[i, j)`,那么只要翻转 `ans[i: j + 1]` 即可。
54+
55+
因此,遍历字符串 `s`,找出所有的连续 `D` 子数组区间,将其翻转。
56+
57+
时间复杂度 $O(n)$,其中 $n$ 表示字符串 `s` 的长度。
58+
4959
<!-- tabs:start -->
5060

5161
### **Python3**
5262

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

5565
```python
56-
66+
class Solution:
67+
def findPermutation(self, s: str) -> List[int]:
68+
n = len(s)
69+
ans = list(range(1, n + 2))
70+
i = 0
71+
while i < n:
72+
j = i
73+
while j < n and s[j] == 'D':
74+
j += 1
75+
ans[i: j + 1] = ans[i: j + 1][::-1]
76+
i = max(i + 1, j)
77+
return ans
5778
```
5879

5980
### **Java**
6081

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

6384
```java
85+
class Solution {
86+
public int[] findPermutation(String s) {
87+
int n = s.length();
88+
int[] ans = new int[n + 1];
89+
for (int i = 0; i < n + 1; ++i) {
90+
ans[i] = i + 1;
91+
}
92+
int i = 0;
93+
while (i < n) {
94+
int j = i;
95+
while (j < n && s.charAt(j) == 'D') {
96+
++j;
97+
}
98+
reverse(ans, i, j);
99+
i = Math.max(i + 1, j);
100+
}
101+
return ans;
102+
}
103+
104+
private void reverse(int[] arr, int i, int j) {
105+
for (; i < j; ++i, --j) {
106+
int t = arr[i];
107+
arr[i] = arr[j];
108+
arr[j] = t;
109+
}
110+
}
111+
}
112+
```
113+
114+
### **C++**
115+
116+
```cpp
117+
class Solution {
118+
public:
119+
vector<int> findPermutation(string s) {
120+
int n = s.size();
121+
vector<int> ans(n + 1);
122+
iota(ans.begin(), ans.end(), 1);
123+
int i = 0;
124+
while (i < n) {
125+
int j = i;
126+
while (j < n && s[j] == 'D') {
127+
++j;
128+
}
129+
reverse(ans.begin() + i, ans.begin() + j + 1);
130+
i = max(i + 1, j);
131+
}
132+
return ans;
133+
}
134+
};
135+
```
64136
137+
### **Go**
138+
139+
```go
140+
func findPermutation(s string) []int {
141+
n := len(s)
142+
ans := make([]int, n+1)
143+
for i := range ans {
144+
ans[i] = i + 1
145+
}
146+
i := 0
147+
for i < n {
148+
j := i
149+
for ; j < n && s[j] == 'D'; j++ {
150+
}
151+
reverse(ans, i, j)
152+
i = max(i+1, j)
153+
}
154+
return ans
155+
}
156+
157+
func reverse(arr []int, i, j int) {
158+
for ; i < j; i, j = i+1, j-1 {
159+
arr[i], arr[j] = arr[j], arr[i]
160+
}
161+
}
162+
163+
func max(a, b int) int {
164+
if a > b {
165+
return a
166+
}
167+
return b
168+
}
65169
```
66170

67171
### **...**

solution/0400-0499/0484.Find Permutation/README_EN.md

+95-1
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,107 @@
4545
### **Python3**
4646

4747
```python
48-
48+
class Solution:
49+
def findPermutation(self, s: str) -> List[int]:
50+
n = len(s)
51+
ans = list(range(1, n + 2))
52+
i = 0
53+
while i < n:
54+
j = i
55+
while j < n and s[j] == 'D':
56+
j += 1
57+
ans[i: j + 1] = ans[i: j + 1][::-1]
58+
i = max(i + 1, j)
59+
return ans
4960
```
5061

5162
### **Java**
5263

5364
```java
65+
class Solution {
66+
public int[] findPermutation(String s) {
67+
int n = s.length();
68+
int[] ans = new int[n + 1];
69+
for (int i = 0; i < n + 1; ++i) {
70+
ans[i] = i + 1;
71+
}
72+
int i = 0;
73+
while (i < n) {
74+
int j = i;
75+
while (j < n && s.charAt(j) == 'D') {
76+
++j;
77+
}
78+
reverse(ans, i, j);
79+
i = Math.max(i + 1, j);
80+
}
81+
return ans;
82+
}
83+
84+
private void reverse(int[] arr, int i, int j) {
85+
for (; i < j; ++i, --j) {
86+
int t = arr[i];
87+
arr[i] = arr[j];
88+
arr[j] = t;
89+
}
90+
}
91+
}
92+
```
93+
94+
### **C++**
95+
96+
```cpp
97+
class Solution {
98+
public:
99+
vector<int> findPermutation(string s) {
100+
int n = s.size();
101+
vector<int> ans(n + 1);
102+
iota(ans.begin(), ans.end(), 1);
103+
int i = 0;
104+
while (i < n) {
105+
int j = i;
106+
while (j < n && s[j] == 'D') {
107+
++j;
108+
}
109+
reverse(ans.begin() + i, ans.begin() + j + 1);
110+
i = max(i + 1, j);
111+
}
112+
return ans;
113+
}
114+
};
115+
```
54116
117+
### **Go**
118+
119+
```go
120+
func findPermutation(s string) []int {
121+
n := len(s)
122+
ans := make([]int, n+1)
123+
for i := range ans {
124+
ans[i] = i + 1
125+
}
126+
i := 0
127+
for i < n {
128+
j := i
129+
for ; j < n && s[j] == 'D'; j++ {
130+
}
131+
reverse(ans, i, j)
132+
i = max(i+1, j)
133+
}
134+
return ans
135+
}
136+
137+
func reverse(arr []int, i, j int) {
138+
for ; i < j; i, j = i+1, j-1 {
139+
arr[i], arr[j] = arr[j], arr[i]
140+
}
141+
}
142+
143+
func max(a, b int) int {
144+
if a > b {
145+
return a
146+
}
147+
return b
148+
}
55149
```
56150

57151
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
vector<int> findPermutation(string s) {
4+
int n = s.size();
5+
vector<int> ans(n + 1);
6+
iota(ans.begin(), ans.end(), 1);
7+
int i = 0;
8+
while (i < n) {
9+
int j = i;
10+
while (j < n && s[j] == 'D') {
11+
++j;
12+
}
13+
reverse(ans.begin() + i, ans.begin() + j + 1);
14+
i = max(i + 1, j);
15+
}
16+
return ans;
17+
}
18+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
func findPermutation(s string) []int {
2+
n := len(s)
3+
ans := make([]int, n+1)
4+
for i := range ans {
5+
ans[i] = i + 1
6+
}
7+
i := 0
8+
for i < n {
9+
j := i
10+
for ; j < n && s[j] == 'D'; j++ {
11+
}
12+
reverse(ans, i, j)
13+
i = max(i+1, j)
14+
}
15+
return ans
16+
}
17+
18+
func reverse(arr []int, i, j int) {
19+
for ; i < j; i, j = i+1, j-1 {
20+
arr[i], arr[j] = arr[j], arr[i]
21+
}
22+
}
23+
24+
func max(a, b int) int {
25+
if a > b {
26+
return a
27+
}
28+
return b
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public int[] findPermutation(String s) {
3+
int n = s.length();
4+
int[] ans = new int[n + 1];
5+
for (int i = 0; i < n + 1; ++i) {
6+
ans[i] = i + 1;
7+
}
8+
int i = 0;
9+
while (i < n) {
10+
int j = i;
11+
while (j < n && s.charAt(j) == 'D') {
12+
++j;
13+
}
14+
reverse(ans, i, j);
15+
i = Math.max(i + 1, j);
16+
}
17+
return ans;
18+
}
19+
20+
private void reverse(int[] arr, int i, int j) {
21+
for (; i < j; ++i, --j) {
22+
int t = arr[i];
23+
arr[i] = arr[j];
24+
arr[j] = t;
25+
}
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def findPermutation(self, s: str) -> List[int]:
3+
n = len(s)
4+
ans = list(range(1, n + 2))
5+
i = 0
6+
while i < n:
7+
j = i
8+
while j < n and s[j] == 'D':
9+
j += 1
10+
ans[i: j + 1] = ans[i: j + 1][::-1]
11+
i = max(i + 1, j)
12+
return ans

0 commit comments

Comments
 (0)