Leetcode - Reverse Vowels of a String

My code:

public class Solution {
    public String reverseVowels(String s) {
        if (s == null || s.length() == 0) {
            return s;
        }
        
        char[] arr = s.toCharArray();
        int i = 0;
        int j = arr.length - 1;
        while (i < j) {
            while (i < j && !isVowel(s.charAt(i))) {
                i++;
            }
            while (j > i && !isVowel(s.charAt(j))) {
                j--;
            }
            if (i >= j) {
                break;
            }
            else {
                char temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
                i++;
                j--;
            }
        }
        
        return new String(arr);
    }
    
    private boolean isVowel(char c) {
        if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
            return true;
        }
        else if (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') {
            return true;
        }
        else {
            return false;
        }
    }
}

简单题。
vowel 元音字母
aeiou
AEIOU
大写字母也需要考虑。

Anyway, Good luck, Richardo! — 08/27/2016

    原文作者:Richardo92
    原文地址: https://www.jianshu.com/p/7004a2345cb7#comments
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞