leetcode_反转字符串的元音字母

还是双指针

 1 class Solution {
 2     public String reverseVowels(String s) {
 3         char ch[] = s.toCharArray();
 4         int i=0;
 5         int j=s.length()-1;
 6         while(i<j){
 7             if(isYuan(ch[i])){
 8                 if(isYuan(ch[j])){
 9                     swap(ch,i,j);
10                      i++;
11                      j--;
12                 }
13                 else{
14                     j--;
15                 }
16             }
17             else{
18                 if(isYuan(ch[j])){
19                     i++;
20                 }
21                 else{
22                     i++;
23                     j--;
24                 }
25             }
26         }
27         
28         return new String(ch);
29         
30     }
31     public void swap(char ch[],int i,int j){
32         char temp = ch[i];
33         ch[i]=ch[j];
34         ch[j]=temp;
35     }
36     
37     public boolean isYuan(char c){
38          if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||c=='A'||c=='E'||c=='I'||c=='O'||c=='U')  return true;
39          return false;
40     }
41 }

 

posted @ 2019-09-22 20:20  chyblogs  阅读(154)  评论(0)    收藏  举报