// O(1)空间复杂度
// 反转列表中的元音字母, 返回反转后的字符串
//
// 例如:
// 输入: aeiou
// 输出:uoiea
//
// 输入: hellio
// 输出:hollie
// '''
// Java
//class Solution {
// public void reverseVowel(char[] s) {
//
// }
//}
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
char[] ch = new char[]{'h', 'e', 'a', 'e','i', 'o'};
revertStr(ch);
StringBuilder sb = new StringBuilder();
for (char c : ch) {
sb.append(c);
}
System.out.println(sb.toString());
}
private static void revertStr(char[] ch) {
// aeiou
List<Character> list = Arrays.asList('a', 'e', 'i', 'o', 'u');
int start = 0, end = ch.length - 1;
while (start < end) {
while (!list.contains(ch[start])) {
start++;
}
while (!list.contains(ch[end])) {
end--;
}
char temp = ch[start];
ch[start] = ch[end];
ch[end] = temp;
start++;
end--;
}
}
}
华为技术2面
最新推荐文章于 2026-05-19 09:39:00 发布
该博客介绍了一个Java方法,用于在O(1)空间复杂度下反转字符串中的元音字母。通过遍历字符串两端并检查字符是否为元音,实现了字符串的高效反转。示例包括输入'aeiou'得到'uoiea',以及输入'hellio'得到'hollie'。
8885

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



