【leetcode80】Reverse Vowels of a String(元音字母倒叙)
题目描述:
写一个函数,实现输入一个字符串,然后把其中的元音字母倒叙
注意
元音字母包含大小写,元音字母有五个a,e,i,o,u
原文描述:
Write a function that takes a string as input and reverse only the vowels of a string.
Example 1:
Given s = “hello”, return “holle”.
Example 2:
Given s = “leetcode”, return “leotcede”.
Note:
The vowels does not include the letter “y”.
思路:
- 使用HashSet的数据结构存储者10个字符
- 遍历字符串,使用int【s.length】记录元音的位置,元音的个数为n
- 遍历字符串,根据上一个数组,把所有的元音字母(索引为i)换成string.charAt(n-i-1)处的字符
代码:
public class Solution {
public String reverseVowels(String s) {
if(s == null){
return null;
}
int[] array = new int[s.length()];
int index = 0;
HashSet<Character> vowel = new HashSet<Character>();
vowel.add('a');
vowel.add('e');
vowel.add('i');
vowel.add('o');
vowel.add('u');
vowel.add('A');
vowel.add('E');
vowel.add('I');
vowel.add('O');
vowel.add('U');
for (int i = 0; i < s.length(); i++) {
if (vowel.contains(s.charAt(i))) {
array[index] = i;
index++;
}
}
char[] result = new char[s.length()];
result = s.toCharArray();
for (int i = 0; i < index; i++) {
result[array[i]] = s.charAt(array[index - i - 1]);
}
return String.valueOf(result);
}
}
更多的leetcode的经典算法,查看我的leetcode专栏,链接如下:
我的微信二维码如下,欢迎交流讨论
欢迎关注《IT面试题汇总》微信订阅号。每天推送经典面试题和面试心得技巧,都是干货!
微信订阅号二维码如下:
【leetcode80】Reverse Vowels of a String(元音字母倒叙)的更多相关文章
- 345. Reverse Vowels of a String(C++)
345. Reverse Vowels of a String Write a function that takes a string as input and reverse only the v ...
- 【leetcode】345. Reverse Vowels of a String
problem 345. Reverse Vowels of a String class Solution { public: string reverseVowels(string s) { , ...
- 345. Reverse Vowels of a String - LeetCode
Question 345. Reverse Vowels of a String Solution 思路:交换元音,第一次遍历,先把出现元音的索引位置记录下来,第二遍遍历元音的索引并替换. Java实 ...
- 345. Reverse Vowels of a String【easy】
345. Reverse Vowels of a String[easy] Write a function that takes a string as input and reverse only ...
- LeetCode_345. Reverse Vowels of a String
345. Reverse Vowels of a String Easy Write a function that takes a string as input and reverse only ...
- [LeetCode] Reverse Vowels of a String 翻转字符串中的元音字母
Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Giv ...
- [Swift]LeetCode345. 反转字符串中的元音字母 | Reverse Vowels of a String
Write a function that takes a string as input and reverse only the vowels of a string. Example 1: In ...
- C#LeetCode刷题之#345-反转字符串中的元音字母(Reverse Vowels of a String)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3935 访问. 编写一个函数,以字符串作为输入,反转该字符串中的元 ...
- 345. Reverse Vowels of a String翻转字符串中的元音字母
[抄题]: Write a function that takes a string as input and reverse only the vowels of a string. Example ...
随机推荐
- mysql常见的优化需要注意的点
1.explain分析explian引用索引基数show indexes from table_name;主键索引具有最好的基数 测试时 不走缓存SELECT SQL_NO_CACHE id from ...
- 解析配置文件redis.conf
units单位: # 1k => 1000 bytes # 1kb => 1024 bytes # 1m => 1000000 bytes # 1mb => 1024*1024 ...
- leetcode刷题笔记08 字符串转整数 (atoi)
题目描述 实现 atoi,将字符串转为整数. 在找到第一个非空字符之前,需要移除掉字符串中的空格字符.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字符即 ...
- Android Studio精彩案例(六)《使用一个Demo涵盖补间动画所有知识》
转载本专栏文章,请注明出处,尊重原创 .文章博客地址:道龙的博客 元旦假期里,闲的无事,看到美团加载数据的动画,就突想写个Demo把动画知识集成一下.后来想了想,还是直接用一个Demo来把所有动画知识 ...
- Bootstrap3 排版-改变大小写
通过这几个类可以改变文本的大小写. <p class="text-lowercase">Lowercased text.</p> <p class=& ...
- RxJava操作符(09-算术/聚合操作&连接操作)
转载请标明出处: http://blog.csdn.net/xmxkf/article/details/51692493 本文出自:[openXu的博客] 目录: 算术聚合 Count Concat ...
- 剑指Offer——关于劳动合同,这6件事毕业生必须知道!
剑指Offer--关于劳动合同,这6件事毕业生必须知道! 求职找工作,不少人拿到劳动合同的那刻,可能连合同内容都没看清,就挥着笔杆子"签签签".别急!劳动合同包含哪些条款你清楚 ...
- Python 通过继承实现标准对象的子类
idict是dict的子类,它的键值和属性是同步的,并且有强大的默认值机制. 例如,假设x是idict的一个实例,且x['a']['b']=12,则有x.a.b=12.反之亦然; 假设'c'不在x的键 ...
- 【Unity Shader】2D动态云彩
写在前面 赶在年前写一篇文章.之前翻看2015年的SIGGRAPH Course(关于渲染的可以去selfshadow的博客里找到,很全)的时候看到了关于体积云的渲染.这个课程讲述了开发者为游戏< ...
- Ajax框架,DWR介绍,应用,例子
使用Ajax框架 1. 简化JavaScript的开发难度 2. 解决浏览器的兼容性问题 3. 简化开发流程 常用Ajax框架 Prototype 一个纯粹的JavaScript函数库,对Ajax提供 ...