当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- How to get started with rabbitmq
- 如何将 PyTorch Lightning 模型部署到生产中
- C++邻接矩阵
- Factory pattern pattern pattern (simple factory, factory method, abstract factory pattern)
- JVM真香系列:轻松理解class文件到虚拟机(上)
- 为什么需要使用API管理平台
- Linked list
- Are there many Python application scenarios?
- Iterm2 configuration and beautification
- Aprelu: cross border application, adaptive relu | IEEE tie 2020 for machine fault detection
猜你喜欢

The interface testing tool eolinker makes post request

How to deploy pytorch lightning model to production

Leetcode-11: container with the most water

B. protocal has 7000eth assets in one week!

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

Chapter five

教你如何 分析 Android ANR 问题

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

Python的特性与搭建环境

VIM Introduction Manual, (vs Code)
随机推荐
云计算之路-出海记-小目标:Hello World from .NET 5.0 on AWS
APP 莫名崩溃,开始以为是 Header 中 name 大小写的锅,最后发现原来是容器的错!
你有没有想过为什么交易和退款要拆开不同的表
服务器性能监控神器nmon使用介绍
老大问我:“建表为啥还设置个自增 id ?用流水号当主键不正好么?”
Execution of SQL statement
Decorator (2)
Factory pattern pattern pattern (simple factory, factory method, abstract factory pattern)
Copy the picture
分库分表的几种常见玩法及如何解决跨库查询等问题
Swagger介绍和应用
如何让脚本同时兼容Python2和Python3?
简单介绍c#通过代码开启或关闭防火墙示例
采用注解+拦截器的方式进行异步执行的实现方式
程序员都应该知道的URI,一文帮你全面了解
LeetCode-15:三数之和
实现图片的复制
上线1周,B.Protocal已有7000ETH资产!
Aprelu: cross border application, adaptive relu | IEEE tie 2020 for machine fault detection
AQS 都看完了,Condition 原理可不能少!