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 String reverseVowels(String s) {
    ArrayList<Character> vowList = new ArrayList<Character>();
    vowList.add('a');
    vowList.add('e');
    vowList.add('i');
    vowList.add('o');
    vowList.add('u');
    vowList.add('A');
    vowList.add('E');
    vowList.add('I');
    vowList.add('O');
    vowList.add('U');
 
    char[] arr = s.toCharArray();//将字符串转换为arraylist数据
 
    int i=0; 
    int j=s.length()-1;
 
    while(i<j){
        if(!vowList.contains(arr[i])){
            i++;
            continue;
        }
 
        if(!vowList.contains(arr[j])){
            j--;
            continue;
        }
 
        char t = arr[i];
        arr[i]=arr[j];
        arr[j]=t;
 
        i++;
        j--; 
    }
 
    return new String(arr);
}

 

posted @ 2016-12-19 15:39  不思蜀  阅读(146)  评论(0编辑  收藏  举报