Skip to content

Commit f00b338

Browse files
authored
feat: add solutions to lc problems: No.0344,0345 (doocs#1410)
* No.0344.Reverse String * No.0345.Reverse Vowels of a String
1 parent 9b69f29 commit f00b338

File tree

13 files changed

+365
-184
lines changed

13 files changed

+365
-184
lines changed

solution/0300-0399/0344.Reverse String/README.md

+37-12
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@
3838

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

41+
**方法一:双指针**
42+
43+
我们用两个指针 $i$ 和 $j$,初始时分别指向数组的首尾,每次将 $i$ 和 $j$ 对应的元素交换,然后 $i$ 向后移动,$j$ 向前移动,直到 $i$ 和 $j$ 相遇。
44+
45+
时间复杂度 $O(n)$,其中 $n$ 是数组的长度。空间复杂度 $O(1)$。
46+
4147
<!-- tabs:start -->
4248

4349
### **Python3**
@@ -47,9 +53,15 @@
4753
```python
4854
class Solution:
4955
def reverseString(self, s: List[str]) -> None:
50-
"""
51-
Do not return anything, modify s in-place instead.
52-
"""
56+
i, j = 0, len(s) - 1
57+
while i < j:
58+
s[i], s[j] = s[j], s[i]
59+
i, j = i + 1, j - 1
60+
```
61+
62+
```python
63+
class Solution:
64+
def reverseString(self, s: List[str]) -> None:
5365
s[:] = s[::-1]
5466
```
5567

@@ -75,8 +87,9 @@ class Solution {
7587
class Solution {
7688
public:
7789
void reverseString(vector<char>& s) {
78-
for (int i = 0, j = s.size() - 1; i < j; ++i, --j)
79-
swap(s[i], s[j]);
90+
for (int i = 0, j = s.size() - 1; i < j;) {
91+
swap(s[i++], s[j--]);
92+
}
8093
}
8194
};
8295
```
@@ -91,6 +104,19 @@ func reverseString(s []byte) {
91104
}
92105
```
93106

107+
### **TypeScript**
108+
109+
```ts
110+
/**
111+
Do not return anything, modify s in-place instead.
112+
*/
113+
function reverseString(s: string[]): void {
114+
for (let i = 0, j = s.length - 1; i < j; ++i, --j) {
115+
[s[i], s[j]] = [s[j], s[i]];
116+
}
117+
}
118+
```
119+
94120
### **JavaScript**
95121

96122
```js
@@ -110,13 +136,12 @@ var reverseString = function (s) {
110136
```rust
111137
impl Solution {
112138
pub fn reverse_string(s: &mut Vec<char>) {
113-
let n = s.len();
114-
let mut l = 0;
115-
let mut r = n - 1;
116-
while l < r {
117-
s.swap(l, r);
118-
l += 1;
119-
r -= 1;
139+
let mut i = 0;
140+
let mut j = s.len() - 1;
141+
while i < j {
142+
s.swap(i, j);
143+
i += 1;
144+
j -= 1;
120145
}
121146
}
122147
}

solution/0300-0399/0344.Reverse String/README_EN.md

+31-12
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,15 @@
3333
```python
3434
class Solution:
3535
def reverseString(self, s: List[str]) -> None:
36-
"""
37-
Do not return anything, modify s in-place instead.
38-
"""
36+
i, j = 0, len(s) - 1
37+
while i < j:
38+
s[i], s[j] = s[j], s[i]
39+
i, j = i + 1, j - 1
40+
```
41+
42+
```python
43+
class Solution:
44+
def reverseString(self, s: List[str]) -> None:
3945
s[:] = s[::-1]
4046
```
4147

@@ -59,8 +65,9 @@ class Solution {
5965
class Solution {
6066
public:
6167
void reverseString(vector<char>& s) {
62-
for (int i = 0, j = s.size() - 1; i < j; ++i, --j)
63-
swap(s[i], s[j]);
68+
for (int i = 0, j = s.size() - 1; i < j;) {
69+
swap(s[i++], s[j--]);
70+
}
6471
}
6572
};
6673
```
@@ -75,6 +82,19 @@ func reverseString(s []byte) {
7582
}
7683
```
7784

85+
### **TypeScript**
86+
87+
```ts
88+
/**
89+
Do not return anything, modify s in-place instead.
90+
*/
91+
function reverseString(s: string[]): void {
92+
for (let i = 0, j = s.length - 1; i < j; ++i, --j) {
93+
[s[i], s[j]] = [s[j], s[i]];
94+
}
95+
}
96+
```
97+
7898
### **JavaScript**
7999

80100
```js
@@ -94,13 +114,12 @@ var reverseString = function (s) {
94114
```rust
95115
impl Solution {
96116
pub fn reverse_string(s: &mut Vec<char>) {
97-
let n = s.len();
98-
let mut l = 0;
99-
let mut r = n - 1;
100-
while l < r {
101-
s.swap(l, r);
102-
l += 1;
103-
r -= 1;
117+
let mut i = 0;
118+
let mut j = s.len() - 1;
119+
while i < j {
120+
s.swap(i, j);
121+
i += 1;
122+
j -= 1;
104123
}
105124
}
106125
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
class Solution {
2-
public:
3-
void reverseString(vector<char>& s) {
4-
for (int i = 0, j = s.size() - 1; i < j; ++i, --j)
5-
swap(s[i], s[j]);
6-
}
1+
class Solution {
2+
public:
3+
void reverseString(vector<char>& s) {
4+
for (int i = 0, j = s.size() - 1; i < j;) {
5+
swap(s[i++], s[j--]);
6+
}
7+
}
78
};
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
class Solution:
2-
def reverseString(self, s: List[str]) -> None:
3-
"""
4-
Do not return anything, modify s in-place instead.
5-
"""
6-
s[:] = s[::-1]
1+
class Solution:
2+
def reverseString(self, s: List[str]) -> None:
3+
i, j = 0, len(s) - 1
4+
while i < j:
5+
s[i], s[j] = s[j], s[i]
6+
i, j = i + 1, j - 1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
impl Solution {
2-
pub fn reverse_string(s: &mut Vec<char>) {
3-
let n = s.len();
4-
let mut l = 0;
5-
let mut r = n - 1;
6-
while l < r {
7-
s.swap(l, r);
8-
l += 1;
9-
r -= 1;
10-
}
11-
}
12-
}
1+
impl Solution {
2+
pub fn reverse_string(s: &mut Vec<char>) {
3+
let mut i = 0;
4+
let mut j = s.len() - 1;
5+
while i < j {
6+
s.swap(i, j);
7+
i += 1;
8+
j -= 1;
9+
}
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
Do not return anything, modify s in-place instead.
3+
*/
4+
function reverseString(s: string[]): void {
5+
for (let i = 0, j = s.length - 1; i < j; ++i, --j) {
6+
[s[i], s[j]] = [s[j], s[i]];
7+
}
8+
}

solution/0300-0399/0345.Reverse Vowels of a String/README.md

+90-41
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,13 @@
3838

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

41-
将字符串转为字符数组(或列表),定义双指针 i、j,分别指向数组(列表)头部和尾部,当 i、j 指向的字符均为元音字母时,进行交换。
41+
**方法一:双指针**
4242

43-
依次遍历,当 `i >= j` 时,遍历结束。将字符数组(列表)转为字符串返回即可。
43+
我们可以用两个指针 $i$ 和 $j$,初始时分别指向字符串的首尾。
44+
45+
每次循环判断 $i$ 指向的字符是否是元音字母,如果不是则向后移动 $i$;同理,判断 $j$ 指向的字符是否是元音字母,如果不是则向前移动 $j$。如果此时 $i \lt j$,那么 $i$ 和 $j$ 指向的字符都是元音字母,交换这两个字符。然后向后移动 $i$,向前移动 $j$。继续上述操作,直到 $i \ge j$。
46+
47+
时间复杂度 $O(n)$,其中 $n$ 是字符串的长度。空间复杂度 $O(|\Sigma|)$,其中 $\Sigma$ 是字符集的大小。
4448

4549
<!-- tabs:start -->
4650

@@ -51,20 +55,18 @@
5155
```python
5256
class Solution:
5357
def reverseVowels(self, s: str) -> str:
54-
vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'}
58+
vowels = "aeiouAEIOU"
5559
i, j = 0, len(s) - 1
56-
chars = list(s)
60+
cs = list(s)
5761
while i < j:
58-
if chars[i] not in vowels:
62+
while i < j and cs[i] not in vowels:
5963
i += 1
60-
continue
61-
if chars[j] not in vowels:
64+
while i < j and cs[j] not in vowels:
6265
j -= 1
63-
continue
64-
chars[i], chars[j] = chars[j], chars[i]
65-
i += 1
66-
j -= 1
67-
return ''.join(chars)
66+
if i < j:
67+
cs[i], cs[j] = cs[j], cs[i]
68+
i, j = i + 1, j - 1
69+
return "".join(cs)
6870
```
6971

7072
### **Java**
@@ -74,55 +76,102 @@ class Solution:
7476
```java
7577
class Solution {
7678
public String reverseVowels(String s) {
77-
Set<Character> vowels
78-
= new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'));
79-
int i = 0, j = s.length() - 1;
80-
char[] chars = s.toCharArray();
79+
boolean[] vowels = new boolean[128];
80+
for (char c : "aeiouAEIOU".toCharArray()) {
81+
vowels[c] = true;
82+
}
83+
char[] cs = s.toCharArray();
84+
int i = 0, j = cs.length - 1;
8185
while (i < j) {
82-
if (!vowels.contains(chars[i])) {
86+
while (i < j && !vowels[cs[i]]) {
8387
++i;
84-
continue;
8588
}
86-
if (!vowels.contains(chars[j])) {
89+
while (i < j && !vowels[cs[j]]) {
90+
--j;
91+
}
92+
if (i < j) {
93+
char t = cs[i];
94+
cs[i] = cs[j];
95+
cs[j] = t;
96+
++i;
8797
--j;
88-
continue;
8998
}
90-
char t = chars[i];
91-
chars[i] = chars[j];
92-
chars[j] = t;
93-
++i;
94-
--j;
9599
}
96-
return new String(chars);
100+
return String.valueOf(cs);
97101
}
98102
}
99103
```
100104

105+
### **C++**
106+
107+
```cpp
108+
class Solution {
109+
public:
110+
string reverseVowels(string s) {
111+
bool vowels[128];
112+
memset(vowels, false, sizeof(vowels));
113+
for (char c : "aeiouAEIOU") {
114+
vowels[c] = true;
115+
}
116+
int i = 0, j = s.size() - 1;
117+
while (i < j) {
118+
while (i < j && !vowels[s[i]]) {
119+
++i;
120+
}
121+
while (i < j && !vowels[s[j]]) {
122+
--j;
123+
}
124+
if (i < j) {
125+
swap(s[i++], s[j--]);
126+
}
127+
}
128+
return s;
129+
}
130+
};
131+
```
132+
101133
### **Go**
102134
103135
```go
104136
func reverseVowels(s string) string {
105-
left, right := 0, len(s)-1
106-
a := []byte(s)
107-
for left < right {
108-
for left < right && !isVowel(a[left]) {
109-
left++
137+
vowels := [128]bool{}
138+
for _, c := range "aeiouAEIOU" {
139+
vowels[c] = true
140+
}
141+
cs := []byte(s)
142+
i, j := 0, len(cs)-1
143+
for i < j {
144+
for i < j && !vowels[cs[i]] {
145+
i++
110146
}
111-
for left < right && !isVowel(a[right]) {
112-
right--
147+
for i < j && !vowels[cs[j]] {
148+
j--
113149
}
114-
if left != right && isVowel(a[left]) && isVowel(a[right]) {
115-
a[left], a[right] = a[right], a[left]
116-
left++
117-
right--
150+
if i < j {
151+
cs[i], cs[j] = cs[j], cs[i]
152+
i, j = i+1, j-1
118153
}
119154
}
120-
return string(a)
155+
return string(cs)
121156
}
157+
```
158+
159+
### **TypeScript**
122160

123-
func isVowel(b byte) bool {
124-
return b == 'a' || b == 'e' || b == 'i' || b == 'o' || b == 'u' ||
125-
b == 'A' || b == 'E' || b == 'I' || b == 'O' || b == 'U'
161+
```ts
162+
function reverseVowels(s: string): string {
163+
const vowels = new Set(['a', 'e', 'i', 'o', 'u']);
164+
const cs = s.split('');
165+
for (let i = 0, j = cs.length - 1; i < j; ++i, --j) {
166+
while (i < j && !vowels.has(cs[i].toLowerCase())) {
167+
++i;
168+
}
169+
while (i < j && !vowels.has(cs[j].toLowerCase())) {
170+
--j;
171+
}
172+
[cs[i], cs[j]] = [cs[j], cs[i]];
173+
}
174+
return cs.join('');
126175
}
127176
```
128177

0 commit comments

Comments
 (0)