陣列//反轉字串中的母音字母
阿新 • • 發佈:2018-12-19
編寫一個函式,以字串作為輸入,反轉該字串中的母音字母。
示例 1:
輸入: "hello" 輸出: "holle"
示例 2:
輸入: "leetcode" 輸出: "leotcede"
說明:
母音字母不包含字母"y"。
class Solution { public String reverseVowels(String s) { if(s == ""||s == null) return s; int i = 0, j = s.length()-1; char []str = s.toCharArray(); while(i < j){ while(i < j&&!judgeVowel(str[i])) i++; while(i < j&&!judgeVowel(str[j])) j--; if(i < j){ char temp = str[j]; str[j] = str[i]; str[i] = temp; i++; j--; } } return new String(str); } public boolean judgeVowel(char c){ return c == 'a' | c == 'e' | c == 'i' | c == 'o' | c == 'u' | c == 'A' | c == 'E' | c == 'I' | c == 'O' | c == 'U'; } }