当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
猜你喜欢

Octave基本语法

使用递增计数器的线程同步工具 —— 信号量,它的原理是什么样子的?

Installation record of SAP s / 4hana 2020

STS安装

CMS garbage collector

The road of cloud computing - going to sea - small goal: Hello world from. Net 5.0 on AWS

上线1周,B.Protocal已有7000ETH资产!

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!

How to deploy pytorch lightning model to production

SAP S/4HANA 2020安装实录
随机推荐
Are there many Python application scenarios?
Chapter five
Get the first cover image of video through canvas
SQL语句的执行
android开发中提示:requires permission android.permission write_settings解决方法
C / C + + Programming Notes: pointer! Understand pointer from memory, let you understand pointer completely
Combine theory with practice to understand CORS thoroughly
Save code
服务器性能监控神器nmon使用介绍
代码保存
一堆代码忘了缩进?快捷方式教你无忧无虑!
Swagger介绍和应用
几行代码轻松实现跨系统传递 traceId,再也不用担心对不上日志了!
First development of STC to stm32
Solve the problem that the value of new date() of JS in IE and Firefox is invalid date and Nan Nan
Share API on the web
基于链表的有界阻塞队列 —— LinkedBlockingQueue
Mycat搭建
CMS垃圾收集器
MYCAT build