345_反转字符串中的元音字母
小于 1 分钟
345_反转字符串中的元音字母简单
解题思路:
- 双指针
class Solution {
public String reverseVowels(String s) {
char[] vowels = new char[]{'a','e','i','o','u'};
Set<Character> set = new HashSet<>();
for (char c : vowels) {
set.add(c);
set.add(Character.toUpperCase(c));
}
StringBuilder res = new StringBuilder(s);
int left = 0,right = res.length() - 1;
while (left < right){
while(!set.contains(res.charAt(left)) && (left < right)) left++;
while(!set.contains(res.charAt(right)) && (left < right)) right--;
char temp = res.charAt(right);
res.setCharAt(right--,res.charAt(left));
res.setCharAt(left++,temp);
}
return res.toString();
}
}
Powered by Waline v2.15.5