345. Reverse Vowels of a String
- Total Accepted: 55543
- Total Submissions: 150293
- Difficulty: Easy
- Contributors: Admin
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".
Note:
The vowels does not include the letter "y".
没啥好说的:
public class Solution {
public String reverseVowels(String s) {
int len = s.length();
int left = 0;
int right = len-1;
char[] cs = s.toCharArray();
while(left<right){
while(left<len && !helper(s.charAt(left)))left++;
while(right>=0 && !helper(s.charAt(right)))right--;
if(left<right){
if(cs[left] != cs[right]){
char tem = cs[left];
cs[left] = cs[right];
cs[right] = tem;
}
left++;
right--;
}
}
return new String(cs);
}
boolean helper(char c){
if(c=='a' || c=='e' || c=='i' || c=='o' || c=='u' || c=='A' || c=='E' || c=='I' || c=='O' || c=='U')return true;
return false;
}
}
time=5ms;