ID | Title | Difficulty | |
---|---|---|---|
Loading... |
345. Reverse Vowels of a String
Easy
LeetCode
Two Pointers, String
Problem
Write a function that takes a string as input and reverse only the vowels of a string.
Example 1:
Input: "hello"
Output: "holle"
Example 2:
Input: "leetcode"
Output: "leotcede"
Code
class Solution {
public String reverseVowels(String s) {
char[] arr = s.toCharArray();
String vow = "aeiouAEIOU";
int left = 0;
int right = s.length() - 1;
while(left < right) {
while(left < right && vow.indexOf(arr[left]) == -1) {
left++;
}
while(left < right && vow.indexOf(arr[right]) == -1) {
right--;
}
char c = arr[left];
arr[left] = arr[right];
arr[right] = c;
left++;
right--;
}
return new String(arr);
}
}
按 <- 键看上一题!
344. Reverse String
按 -> 键看下一题!
346. Moving Average from Data Stream