当前位置:网站首页>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
边栏推荐
- How to make scripts compatible with both Python 2 and python 3?
- Decorator (1)
- The road of cloud computing - going to sea - small goal: Hello world from. Net 5.0 on AWS
- 当我们聊数据质量的时候,我们在聊些什么?
- 解决go get下载包失败问题
- LeetCode-11:盛水最多的容器
- leetcode之反转字符串中的元音字母
- 链表
- 【云服务】阿里云服务器ECS实例规格那么多,如何选型?最佳实践说明
- Come and have a look! What is the relationship between AQS and countdownlatch?
猜你喜欢

非阻塞的无界线程安全队列 —— ConcurrentLinkedQueue

Mobile big data own website precise marketing and accurate customer acquisition

Development and deployment of image classifier application with fastai

信息安全课程设计第一周任务(7条指令的分析)

VIM 入门手册, (VS Code)

STS安装

RabbitMQ快速入门详解

c++11-17 模板核心知识(二)—— 类模板

C / C + + learning diary: original code, inverse code and complement code

装饰器(二)
随机推荐
C/C++学习日记:原码、反码和补码
解决IE、firefox浏览器下JS的new Date()的值为Invalid Date、NaN-NaN的问题
为什么需要使用API管理平台
Introduction skills of big data software learning
leetcode之反转字符串中的元音字母
AI人工智能编程培训学什么课程?
Execution of SQL statement
表连接
Pipedrive如何在每天部署50+次的情况下支持质量发布?
Dynamic programming: maximum subarray
接口测试工具Eolinker进行post请求
Using fastai to develop and deploy image classifier application
解决go get下载包失败问题
[200 interview experience], programmer interview, common interview questions analysis
JVM Zhenxiang series: easy understanding of class files to virtual machines (Part 2)
Nodejs中request出现ESOCKETTIMEDOUT解决方案
STS安装
新手入坑指南:工作原因“重启”Deepin系统,发现真的香啊
200人的程序员面试经验,都在这里了
构造函数和原型