给你一个字符串 s ,仅反转字符串中的所有元音字母,并返回结果字符串。
元音字母包括 ‘a’、‘e’、‘i’、‘o’、‘u’,且可能以大小写两种形式出现。
示例 1:
输入:s = “hello”
输出:“holle”
示例 2:
输入:s = “leetcode”
输出:“leotcede”
提示:
1 <= s.length <= 3 * 105
s 由 可打印的 ASCII 字符组成
解题思路
使用双指针进行左右遍历,其中
class Solution {
public String reverseVowels(String s) {
char[] c =new char[s.length()];
if(s==null)
{
return null;
}
int left= 0;
int right = s.length()-1;
while(right>=left)
{
if(!(s.charAt(left)=='a'||s.charAt(left)=='e'||s.charAt(left)=='i'||s.charAt(left)=='o'||s.charAt(left)=='u'||s.charAt(left)=='A'|| s.charAt(left) == 'E' || s.charAt(left) == 'I' || s.charAt(left) == 'O' || s.charAt(left) == 'U'))
{
c[left]=s.charAt(left);
left++;
}
else if(!(s.charAt(right)=='a'||s.charAt(right)=='e'||s.charAt(right)=='i'||s.charAt(right)=='o'||s.charAt(right)=='u'||s.charAt(right)=='A'|| s.charAt(right) == 'E' || s.charAt(right) == 'I' || s.charAt(right) == 'O' || s.charAt(right) == 'U'))
{
c[right]=s.charAt(right);
right--;
}else
{
c[left]= s.charAt(right);
c[right] = s.charAt(left);
left++;
right--;
}
}
return new String(c);
}
}