当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- A few lines of code can easily transfer traceid across systems, so you don't have to worry about losing the log!
- APP 莫名崩溃,开始以为是 Header 中 name 大小写的锅,最后发现原来是容器的错!
- AQS 都看完了,Condition 原理可不能少!
- On buffer overflow
- 实现图片的复制
- Nodejs中request出现ESOCKETTIMEDOUT解决方案
- 老大问我:“建表为啥还设置个自增 id ?用流水号当主键不正好么?”
- CSP-S 2020 游记
- 选择API管理平台之前要考虑的5个因素
- Octave基本语法
猜你喜欢

Newbe.ObjectVisitor 样例 1

Are there many Python application scenarios?

如何通过Sidecar自定义资源减少Istio代理资源消耗

Web上的分享(Share)API

Factory Pattern模式(简单工厂、工厂方法、抽象工厂模式)

Mycat搭建

程序员都应该知道的URI,一文帮你全面了解

Chapter five

几行代码轻松实现跨系统传递 traceId,再也不用担心对不上日志了!

centos7下安装iperf时出现 make: *** No targets specified and no makefile found. Stop.的解决方案
随机推荐
Introduction and application of swagger
Flink的DataSource三部曲之三:自定义
链表
Test comparison of three domestic cloud databases
Copy on write collection -- copyonwritearraylist
Computer network application layer
大数据软件学习入门技巧
写时复制集合 —— CopyOnWriteArrayList
Linked blocking queue based on linked list
Tasks of the first week of information security curriculum design (analysis of 7 instructions)
API生命周期的5个阶段
AQS 都看完了,Condition 原理可不能少!
写时复制集合 —— CopyOnWriteArrayList
云计算之路-出海记-小目标:Hello World from .NET 5.0 on AWS
国内三大云数据库测试对比
When we talk about data quality, what are we talking about?
Are there many Python application scenarios?
新手入坑指南:工作原因“重启”Deepin系统,发现真的香啊
STC转STM32第一次开发
Factory Pattern模式(简单工厂、工厂方法、抽象工厂模式)