public String reverseVowels(String s){ char[] chars = s.toCharArray(); int i = 0; int j = chars.length - 1; while (i < j) { while (i < j && !isVowel(chars[i])) { ++i; } while (i < j && !isVowel(chars[j])) { --j; } swap(chars, i++, j--); } returnnew String(chars); }
privatebooleanisVowel(char c){ return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U'; }
privatevoidswap(char[] a, int i, int j){ char t = a[i]; a[i] = a[j]; a[j] = t; } }
近期评论