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".
Note:
The vowels does not include the letter "y".
题目含义:反转一个字符串中的元音字母。第一个和最后一个元音字母交换,第二个和倒数第二个交换……
1 public String reverseVowels(String s) { 2 char[] list = s.toCharArray(); 3 Set<Character> set = new HashSet<>(); 4 set.add('a'); 5 set.add('e'); 6 set.add('i'); 7 set.add('o'); 8 set.add('u'); 9 set.add('A'); 10 set.add('E'); 11 set.add('I'); 12 set.add('O'); 13 set.add('U'); 14 15 for (int i=0,j=list.length-1;i<j;) 16 { 17 if (!set.contains(list[i])) 18 { 19 i++;continue; 20 } 21 if (!set.contains(list[j])) 22 { 23 j--;continue; 24 } 25 26 char temp = list[i]; 27 list[i]=list[j]; 28 list[j] = temp; 29 i++; 30 j--; 31 } 32 return new String(list); 33 }