当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- Using annotation + interceptor to implement asynchronous execution
- A few lines of code can easily transfer traceid across systems, so you don't have to worry about losing the log!
- Introduction and application of swagger
- CSP-S 2020 游记
- Experiment 1 assignment
- 国内三大云数据库测试对比
- 使用递增计数器的线程同步工具 —— 信号量,它的原理是什么样子的?
- APReLU:跨界应用,用于机器故障检测的自适应ReLU | IEEE TIE 2020
- Five design patterns frequently used in development
- First development of STC to stm32
猜你喜欢
随机推荐
Introduction to nmon
Using annotation + interceptor to implement asynchronous execution
Why need to use API management platform
Python应用场景多不多?
Concurrent linked queue: a non blocking unbounded thread safe queue
服务器性能监控神器nmon使用介绍
The interface testing tool eolinker makes post request
如何通过Sidecar自定义资源减少Istio代理资源消耗
How to make scripts compatible with both Python 2 and python 3?
Programmers should know the URI, a comprehensive understanding of the article
How does pipedrive support quality publishing with 50 + deployments per day?
上线1周,B.Protocal已有7000ETH资产!
How to get started with rabbitmq
架构中台图
Test comparison of three domestic cloud databases
Dynamic ReLU:微软推出提点神器,可能是最好的ReLU改进 | ECCV 2020
Computer network application layer
C++邻接矩阵
六家公司CTO讲述曾经历的“宕机噩梦”
Dynamic relu: Microsoft's refreshing device may be the best relu improvement | ECCV 2020