Skip to content

Commit 0483eaa

Browse files
committed
feat: update solutions to lc problem: No.0345. Reverse Vowels of a String
1 parent 24bd215 commit 0483eaa

File tree

4 files changed

+71
-140
lines changed

4 files changed

+71
-140
lines changed

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

+25-48
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@
3434

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

37-
将字符串转为字符数组(或列表),定义双指针 p、q,分别指向数组(列表)头部和尾部,当 p、q 指向的字符均为元音字母时,进行交换。
37+
将字符串转为字符数组(或列表),定义双指针 i、j,分别指向数组(列表)头部和尾部,当 i、j 指向的字符均为元音字母时,进行交换。
3838

39-
依次遍历,当 `p >= q` 时,遍历结束。将字符数组(列表)转为字符串返回即可。
39+
依次遍历,当 `i >= j` 时,遍历结束。将字符数组(列表)转为字符串返回即可。
4040

4141
<!-- tabs:start -->
4242

@@ -47,20 +47,19 @@
4747
```python
4848
class Solution:
4949
def reverseVowels(self, s: str) -> str:
50-
if s is None:
51-
return s
50+
vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'}
51+
i, j = 0, len(s) - 1
5252
chars = list(s)
53-
p, q = 0, len(chars) - 1
54-
while p < q:
55-
if chars[p] not in 'aeiouAEIOU':
56-
p += 1
53+
while i < j:
54+
if chars[i] not in vowels:
55+
i += 1
5756
continue
58-
if chars[q] not in 'aeiouAEIOU':
59-
q -= 1
57+
if chars[j] not in vowels:
58+
j -= 1
6059
continue
61-
chars[p], chars[q] = chars[q], chars[p]
62-
p += 1
63-
q -= 1
60+
chars[i], chars[j] = chars[j], chars[i]
61+
i += 1
62+
j -= 1
6463
return ''.join(chars)
6564
```
6665

@@ -71,47 +70,25 @@ class Solution:
7170
```java
7271
class Solution {
7372
public String reverseVowels(String s) {
74-
if (s == null) {
75-
return s;
76-
}
73+
Set<Character> vowels = new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'));
74+
int i = 0, j = s.length() - 1;
7775
char[] chars = s.toCharArray();
78-
int p = 0, q = chars.length - 1;
79-
while (p < q) {
80-
if (!isVowel(chars[p])) {
81-
++p;
76+
while (i < j) {
77+
if (!vowels.contains(chars[i])) {
78+
++i;
8279
continue;
8380
}
84-
if (!isVowel(chars[q])) {
85-
--q;
81+
if (!vowels.contains(chars[j])) {
82+
--j;
8683
continue;
8784
}
88-
swap(chars, p++, q--);
89-
}
90-
return String.valueOf(chars);
91-
}
92-
93-
private void swap(char[] chars, int i, int j) {
94-
char t = chars[i];
95-
chars[i] = chars[j];
96-
chars[j] = t;
97-
}
98-
99-
private boolean isVowel(char c) {
100-
switch(c) {
101-
case 'a':
102-
case 'e':
103-
case 'i':
104-
case 'o':
105-
case 'u':
106-
case 'A':
107-
case 'E':
108-
case 'I':
109-
case 'O':
110-
case 'U':
111-
return true;
112-
default:
113-
return false;
85+
char t = chars[i];
86+
chars[i] = chars[j];
87+
chars[j] = t;
88+
++i;
89+
--j;
11490
}
91+
return new String(chars);
11592
}
11693
}
11794
```

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

+23-46
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,19 @@
3434
```python
3535
class Solution:
3636
def reverseVowels(self, s: str) -> str:
37-
if s is None:
38-
return s
37+
vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'}
38+
i, j = 0, len(s) - 1
3939
chars = list(s)
40-
p, q = 0, len(chars) - 1
41-
while p < q:
42-
if chars[p] not in 'aeiouAEIOU':
43-
p += 1
40+
while i < j:
41+
if chars[i] not in vowels:
42+
i += 1
4443
continue
45-
if chars[q] not in 'aeiouAEIOU':
46-
q -= 1
44+
if chars[j] not in vowels:
45+
j -= 1
4746
continue
48-
chars[p], chars[q] = chars[q], chars[p]
49-
p += 1
50-
q -= 1
47+
chars[i], chars[j] = chars[j], chars[i]
48+
i += 1
49+
j -= 1
5150
return ''.join(chars)
5251
```
5352

@@ -56,47 +55,25 @@ class Solution:
5655
```java
5756
class Solution {
5857
public String reverseVowels(String s) {
59-
if (s == null) {
60-
return s;
61-
}
58+
Set<Character> vowels = new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'));
59+
int i = 0, j = s.length() - 1;
6260
char[] chars = s.toCharArray();
63-
int p = 0, q = chars.length - 1;
64-
while (p < q) {
65-
if (!isVowel(chars[p])) {
66-
++p;
61+
while (i < j) {
62+
if (!vowels.contains(chars[i])) {
63+
++i;
6764
continue;
6865
}
69-
if (!isVowel(chars[q])) {
70-
--q;
66+
if (!vowels.contains(chars[j])) {
67+
--j;
7168
continue;
7269
}
73-
swap(chars, p++, q--);
74-
}
75-
return String.valueOf(chars);
76-
}
77-
78-
private void swap(char[] chars, int i, int j) {
79-
char t = chars[i];
80-
chars[i] = chars[j];
81-
chars[j] = t;
82-
}
83-
84-
private boolean isVowel(char c) {
85-
switch(c) {
86-
case 'a':
87-
case 'e':
88-
case 'i':
89-
case 'o':
90-
case 'u':
91-
case 'A':
92-
case 'E':
93-
case 'I':
94-
case 'O':
95-
case 'U':
96-
return true;
97-
default:
98-
return false;
70+
char t = chars[i];
71+
chars[i] = chars[j];
72+
chars[j] = t;
73+
++i;
74+
--j;
9975
}
76+
return new String(chars);
10077
}
10178
}
10279
```
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,23 @@
11
class Solution {
22
public String reverseVowels(String s) {
3-
if (s == null) {
4-
return s;
5-
}
3+
Set<Character> vowels = new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'));
4+
int i = 0, j = s.length() - 1;
65
char[] chars = s.toCharArray();
7-
int p = 0, q = chars.length - 1;
8-
while (p < q) {
9-
if (!isVowel(chars[p])) {
10-
++p;
6+
while (i < j) {
7+
if (!vowels.contains(chars[i])) {
8+
++i;
119
continue;
1210
}
13-
if (!isVowel(chars[q])) {
14-
--q;
11+
if (!vowels.contains(chars[j])) {
12+
--j;
1513
continue;
1614
}
17-
swap(chars, p++, q--);
18-
}
19-
return String.valueOf(chars);
20-
}
21-
22-
private void swap(char[] chars, int i, int j) {
23-
char t = chars[i];
24-
chars[i] = chars[j];
25-
chars[j] = t;
26-
}
27-
28-
private boolean isVowel(char c) {
29-
switch(c) {
30-
case 'a':
31-
case 'e':
32-
case 'i':
33-
case 'o':
34-
case 'u':
35-
case 'A':
36-
case 'E':
37-
case 'I':
38-
case 'O':
39-
case 'U':
40-
return true;
41-
default:
42-
return false;
15+
char t = chars[i];
16+
chars[i] = chars[j];
17+
chars[j] = t;
18+
++i;
19+
--j;
4320
}
21+
return new String(chars);
4422
}
4523
}
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
class Solution:
22
def reverseVowels(self, s: str) -> str:
3-
if s is None:
4-
return s
3+
vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'}
4+
i, j = 0, len(s) - 1
55
chars = list(s)
6-
p, q = 0, len(chars) - 1
7-
while p < q:
8-
if chars[p] not in 'aeiouAEIOU':
9-
p += 1
6+
while i < j:
7+
if chars[i] not in vowels:
8+
i += 1
109
continue
11-
if chars[q] not in 'aeiouAEIOU':
12-
q -= 1
10+
if chars[j] not in vowels:
11+
j -= 1
1312
continue
14-
chars[p], chars[q] = chars[q], chars[p]
15-
p += 1
16-
q -= 1
13+
chars[i], chars[j] = chars[j], chars[i]
14+
i += 1
15+
j -= 1
1716
return ''.join(chars)

0 commit comments

Comments
 (0)