LeetCode 345. Reverse Vowels of a String
Description
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”.
class Solution {
public String reverseVowels(String s) {
char[] str = s.toCharArray();
String vol = "aeiouAEIOU";
int lo = 0;
int hi = str.length - 1;
while (lo < hi) {
while (lo < hi && !vol.contains(str[lo]+"")) {
lo++;
}
while (lo < hi && !vol.contains(str[hi] + "")) {
hi--;
}
char tmp = str[lo];
str[lo] = str[hi];
str[hi] = tmp;
lo++;
hi--;
}
return new String(str);
}
}
LeetCode 345:反转字符串中的元音字母
本文介绍了一个LeetCode上的编程挑战——反转字符串中的元音字母。通过实现一个函数来反转输入字符串中的所有元音字母的位置,而保留辅音字母不变。提供了详细的代码示例及解析。
837

被折叠的 条评论
为什么被折叠?



