Write a function that takes a string as input and reverse only the vowels of a string.
Example 1:
Given s = "hello", return "holle".
Example 2:
Given s = "leetcode", return "leotcede".
代碼如下:
1 public class Solution { 2 public String reverseVowels(String s) { 3 ArrayList<Character> list=new ArrayList<>(); 4 char[] ss=s.toCharArray(); 5 list.add('a'); 6 list.add('e'); 7 list.add('i'); 8 list.add('o'); 9 list.add('u'); 10 list.add('A'); 11 list.add('E'); 12 list.add('I'); 13 list.add('O'); 14 list.add('U'); 15 int i=0,j=s.length()-1; 16 while(i<j) 17 { 18 while(!list.contains(ss[i])&&i<j) 19 i++; 20 while(!list.contains(ss[j])&&i<j) 21 j--; 22 23 char c=ss[i]; 24 ss[i]=ss[j]; 25 ss[j]=c; 26 27 i++;j--; 28 } 29 s=String.valueOf(ss); 30 return s; 31 } 32 }