38
38
39
39
<!-- 这里可写通用的实现逻辑 -->
40
40
41
- 将字符串转为字符数组(或列表),定义双指针 i、j,分别指向数组(列表)头部和尾部,当 i、j 指向的字符均为元音字母时,进行交换。
41
+ ** 方法一:双指针 **
42
42
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$ 是字符集的大小。
44
48
45
49
<!-- tabs:start -->
46
50
51
55
``` python
52
56
class Solution :
53
57
def reverseVowels (self , s : str ) -> str :
54
- vowels = { ' a ' , ' e ' , ' i ' , ' o ' , ' u ' , ' A ' , ' E ' , ' I ' , ' O ' , ' U ' }
58
+ vowels = " aeiouAEIOU "
55
59
i, j = 0 , len (s) - 1
56
- chars = list (s)
60
+ cs = list (s)
57
61
while i < j:
58
- if chars [i] not in vowels:
62
+ while i < j and cs [i] not in vowels:
59
63
i += 1
60
- continue
61
- if chars[j] not in vowels:
64
+ while i < j and cs[j] not in vowels:
62
65
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)
68
70
```
69
71
70
72
### ** Java**
@@ -74,55 +76,102 @@ class Solution:
74
76
``` java
75
77
class Solution {
76
78
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 ;
81
85
while (i < j) {
82
- if ( ! vowels. contains(chars[i]) ) {
86
+ while (i < j && ! vowels[cs[i]] ) {
83
87
++ i;
84
- continue ;
85
88
}
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;
87
97
-- j;
88
- continue ;
89
98
}
90
- char t = chars[i];
91
- chars[i] = chars[j];
92
- chars[j] = t;
93
- ++ i;
94
- -- j;
95
99
}
96
- return new String (chars );
100
+ return String . valueOf(cs );
97
101
}
98
102
}
99
103
```
100
104
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
+
101
133
### **Go**
102
134
103
135
```go
104
136
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++
110
146
}
111
- for left < right && !isVowel (a[right]) {
112
- right --
147
+ for i < j && !vowels[cs[j]] {
148
+ j --
113
149
}
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
118
153
}
119
154
}
120
- return string (a )
155
+ return string(cs )
121
156
}
157
+ ```
158
+
159
+ ### ** TypeScript**
122
160
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 (' ' );
126
175
}
127
176
```
128
177
0 commit comments