题目内容 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”.使用双指针,指向待反转的两个元音字符,一个指针从头向尾遍历,一个指针从尾到头遍历。 Java代码 class Solution { private HashSet<Character> vowels = new HashSet<>(Arrays.asList('a','e','i','o','u','A','E','I','O','U')); public String reverseVowels(String s) { if(s.length()==0)return s; int i = 0,j = s.length()-1; char[] result = new char[s.length()]; while(i<=j){ char ci = s.charAt(i); char cj = s.charAt(j); if(!vowels.contains(ci)){ result[i] = ci; i++; } else if(!vowels.contains(cj)){ result[j] = cj; j--; }else { result[i] = cj; result[j] = ci; j--; i++; } } return new String(result); } }