当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
猜你喜欢
写时复制集合 —— CopyOnWriteArrayList
上线1周,B.Protocal已有7000ETH资产!
综合架构的简述
Aprelu: cross border application, adaptive relu | IEEE tie 2020 for machine fault detection
C++邻接矩阵
[200 interview experience], programmer interview, common interview questions analysis
非阻塞的无界线程安全队列 —— ConcurrentLinkedQueue
APP 莫名崩溃,开始以为是 Header 中 name 大小写的锅,最后发现原来是容器的错!
Copy on write collection -- copyonwritearraylist
Fiddler can't grab requests from browsers like Google_ Solution
随机推荐
Five factors to consider before choosing API management platform
VIM Introduction Manual, (vs Code)
Have you ever thought about why the transaction and refund have to be split into different tables
大数据软件学习入门技巧
国内三大云数据库测试对比
Constructors and prototypes
简单介绍c#通过代码开启或关闭防火墙示例
How to make scripts compatible with both Python 2 and python 3?
The road of cloud computing - going to sea - small goal: Hello world from. Net 5.0 on AWS
What are the basic requirements for big data posts?
Dynamic ReLU:微软推出提点神器,可能是最好的ReLU改进 | ECCV 2020
14.Kubenetes简介
上线1周,B.Protocal已有7000ETH资产!
Experiment 1 assignment
Flink's datasource Trilogy 3: customization
Factory Pattern模式(简单工厂、工厂方法、抽象工厂模式)
使用容器存储表格数据
Introduction skills of big data software learning
Programmers should know the URI, a comprehensive understanding of the article
表连接