leetcode 345. 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".

Note:
The vowels does not include the letter "y".

 

public class Solution {
    public String reverseVowels(String s) {
        if(s == "" || s.length() == 0) return "";
        
        int i = 0;
        int j = s.length() - 1;
        
        String vowels = "aeiouAEIOU";
        char[] arr = s.toCharArray();
 
        while(i < j ){
            while(i < j && !vowels.contains(arr[i]+"")){
                i++;
            }
            
            while(i < j && !vowels.contains(arr[j]+"")){
                j--;
            }
            
            char temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
            i++;
            j--;
        }
        return new String(arr);
    }
    
}

 

posted @ 2016-07-26 14:47  冰凌花花~  阅读(112)  评论(0编辑  收藏  举报