Reverse Vowels of a String

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.大小写 2. vowels 指的是元音字母 'a' ,'e', 'i','o','u' 3.题目第意思是two point switch.

 1 public class Solution {
 2     public String reverseVowels(String s) {
 3         if (s == null) {
 4             return null;
 5         }
 6         char[] ch = s.toCharArray();
 7         int left = 0;
 8         int right = ch.length - 1;
 9         while (left < right) {
10             while (left < right && !isVowel(ch[left])) {
11                 ++left;
12             }
13             while (left < right && !isVowel(ch[right])) {
14                 --right;
15             }
16             if (left < right) {
17                 char temp = ch[left];
18                 ch[left] = ch[right];
19                 ch[right] = temp;
20                 ++left;
21                 --right;
22             }
23         }
24         return new String(ch);
25     }
26     
27     private boolean isVowel(char c) {
28         c = Character.toLowerCase(c);
29         if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
30             return true;
31         }
32         return false;
33     }
34 }

 

posted @ 2016-05-08 22:00  YuriFLAG  阅读(118)  评论(0编辑  收藏  举报