345. Reverse Vowels of a String

https://leetcode.com/problems/reverse-vowels-of-a-string/#/description

http://www.cnblogs.com/EdwardLiu/p/6096319.html

public class Solution {
    public String reverseVowels(String s) {
        StringBuffer res = new StringBuffer(s);
        int l=0, r=s.length()-1;
        while (l < r) {
            while (l<r && !isVowel(res.charAt(l))) l++;
            while (l<r && !isVowel(res.charAt(r))) r--;
            if (l == r) break;                     // 双指针的题要记得判断
            char temp = res.charAt(l);             // reverse: setCharAt() 方法
            res.setCharAt(l, res.charAt(r));
            res.setCharAt(r, temp);
            l++;
            r--;
        }
        return res.toString();
    }
    
    public boolean isVowel(char c) {
        if (c=='a' || c=='e' || c=='i' || c=='o' || c=='u'|| c=='A' || c=='E' || c=='I' || c=='O' || c=='U') return true;
        else return false;
    }
}

  

原文地址:https://www.cnblogs.com/apanda009/p/7120880.html