当前位置:网站首页>leetcode之反转字符串中的元音字母
leetcode之反转字符串中的元音字母
2020-11-08 23:48:00 【go4it】
序
本文主要记录一下leetcode之反转字符串中的元音字母
题目
编写一个函数,以字符串作为输入,反转该字符串中的元音字母。
示例 1:
输入:"hello"
输出:"holle"
示例 2:
输入:"leetcode"
输出:"leotcede"
提示:
元音字母不包含字母 "y" 。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/reverse-vowels-of-a-string
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
题解
class Solution {
Set<Character> set = new HashSet(){{
add('a');
add('e');
add('i');
add('o');
add('u');
add('A');
add('E');
add('I');
add('O');
add('U');
}};
public String reverseVowels(String s) {
int i = 0;
int j = s.length() - 1;
char[] chars = s.toCharArray();
while(i < j) {
while (!set.contains(chars[i]) && i<j) {
i++;
}
while (!set.contains(chars[j]) && i<j) {
j--;
}
if (i < j) {
char tmp = chars[i];
chars[i] = chars[j];
chars[j] = tmp;
i++;
j--;
}
}
return new String(chars);
}
}
小结
这里先使用HashSet存放大小写的元音字母,之后使用头尾指针同时对字符串数组进行遍历,当i指向的字符与j指向的字符都是元音时,进行交换同时更新指针,不是元音字符时仅仅更新指针。
doc
版权声明
本文为[go4it]所创,转载请带上原文链接,感谢
https://my.oschina.net/go4it/blog/4708427
边栏推荐
- CountDownLatch 瞬间炸裂!同基于 AQS,凭什么 CyclicBarrier 可以这么秀?
- Programmers should know the URI, a comprehensive understanding of the article
- getBytes之 LengthFieldBasedFrameDecoder服务端解析
- Flink's datasource Trilogy 3: customization
- Looking for better dynamic getter and setter solutions
- 通过canvas获取视频第一帧封面图
- 实验一作业
- 国内三大云数据库测试对比
- 解决go get下载包失败问题
- Using annotation + interceptor to implement asynchronous execution
猜你喜欢
随机推荐
Five design schemes of singleton mode
Iterm2 configuration and beautification
计算机网络 应用层
通过canvas获取视频第一帧封面图
实现图片的复制
Using fastai to develop and deploy image classifier application
选择排序
Development and deployment of image classifier application with fastai
VIM 入门手册, (VS Code)
大数据岗位基础要求有哪些?
写时复制集合 —— CopyOnWriteArrayList
Introduction and application of swagger
CMS garbage collector
基于链表的有界阻塞队列 —— LinkedBlockingQueue
salesforce零基础学习(九十八)Salesforce Connect & External Object
npm install 无响应解决方案
Dynamic programming: maximum subarray
写时复制集合 —— CopyOnWriteArrayList
数据库设计:范式与反范式
Decorator (1)






