File tree Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change 1+ // 大小写!!
2+
3+
4+ public class Solution {
5+ public String reverseVowels (String s ) {
6+ if (s == null )
7+ return s ;
8+
9+ Set <Character > set = new HashSet <>();
10+ set .add ('a' );
11+ set .add ('e' );
12+ set .add ('i' );
13+ set .add ('o' );
14+ set .add ('u' );
15+ set .add ('A' );
16+ set .add ('E' );
17+ set .add ('I' );
18+ set .add ('O' );
19+ set .add ('U' );
20+
21+ StringBuilder tmp = new StringBuilder ();
22+ for (int i = 0 ; i < s .length (); i ++)
23+ tmp .append (s .charAt (i ));
24+
25+ int left = 0 , right = s .length () - 1 ;
26+ while (left < right ){
27+ while (left < s .length () && !set .contains (tmp .charAt (left )))
28+ left ++;
29+ while (right >= 0 && !set .contains (tmp .charAt (right )))
30+ right --;
31+
32+ if (left >= right || left >= s .length () || right < 0 )
33+ break ;
34+
35+ char l = tmp .charAt (left );
36+ char r = tmp .charAt (right );
37+
38+ tmp .deleteCharAt (left );
39+ tmp .insert (left , r );
40+ tmp .deleteCharAt (right );
41+ tmp .insert (right , l );
42+
43+ left ++;
44+ right --;
45+ }
46+
47+ return tmp .toString ();
48+ }
49+ }
You can’t perform that action at this time.
0 commit comments