当前位置:网站首页>The vowels in the inverted string of leetcode
The vowels in the inverted string of leetcode
2020-11-08 23:48:00 【go4it】
order
This article mainly records leetcode Reverse the vowels in a string
subject
Write a function , String as input , Invert the vowels in the string .
Example 1:
Input :"hello"
Output :"holle"
Example 2:
Input :"leetcode"
Output :"leotcede"
Tips :
Vowels do not contain letters "y" .
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/reverse-vowels-of-a-string
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Answer key
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);
}
}
Summary
So let's use this HashSet Storing vowels in uppercase and lowercase , After that, we use the head and tail pointer to traverse the string array at the same time , When i The characters pointed to and j When all the characters pointed to are vowels , Exchange and update the pointer at the same time , Only update the pointer when it is not a vowel character .
doc
版权声明
本文为[go4it]所创,转载请带上原文链接,感谢
边栏推荐
- Chapter five
- First development of STC to stm32
- Why need to use API management platform
- 基于链表的有界阻塞队列 —— LinkedBlockingQueue
- 非阻塞的无界线程安全队列 —— ConcurrentLinkedQueue
- Travel notes of csp-s 2020
- AQS 都看完了,Condition 原理可不能少!
- 大数据岗位基础要求有哪些?
- What are the basic requirements for big data posts?
- Solve the problem that the value of new date() of JS in IE and Firefox is invalid date and Nan Nan
猜你喜欢
centos7下安装iperf时出现 make: *** No targets specified and no makefile found. Stop.的解决方案
Five design patterns frequently used in development
基于链表的有界阻塞队列 —— LinkedBlockingQueue
Using annotation + interceptor to implement asynchronous execution
SQL语句的执行
综合架构的简述
Installation record of SAP s / 4hana 2020
服务器性能监控神器nmon使用介绍
云计算之路-出海记-小目标:Hello World from .NET 5.0 on AWS
Test comparison of three domestic cloud databases
随机推荐
CMS garbage collector
采用注解+拦截器的方式进行异步执行的实现方式
使用递增计数器的线程同步工具 —— 信号量,它的原理是什么样子的?
服务器性能监控神器nmon使用介绍
Experiment 1 assignment
App crashed inexplicably. At first, it thought it was the case of the name in the header. Finally, it was found that it was the fault of the container!
leetcode之反转字符串中的元音字母
c++11-17 模板核心知识(二)—— 类模板
Mobile big data own website precise marketing and accurate customer acquisition
Share API on the web
信息安全课程设计第一周任务(7条指令的分析)
如何让脚本同时兼容Python2和Python3?
Decorator (2)
How to make scripts compatible with both Python 2 and python 3?
Tasks of the first week of information security curriculum design (analysis of 7 instructions)
小议缓冲区溢出
装饰器(二)
上线1周,B.Protocal已有7000ETH资产!
Fiddler无法正常抓取谷歌等浏览器的请求_解决方案
Iterm2 configuration and beautification