【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 ...
随机推荐
- Microsoft Visual Studio 2017 编译最新版 libuv 1.x 并且生成 LIB 和 DLL 两种模式
以为昨天晚上编译通过就可以了,哪知道,早上编译DLL的一车的报错 今天开始逐个解决,终于把引用的问题一亿解决了,具体步骤如下 1 在 Windows 平台下编译出错,显示导出未定义,打开 uv-win ...
- EXISTS的使用详解
.exists的使用场合: exists 用于只能用于子查询,可以替代in,若匹配到结果,则退出内部 查询,并将条件标志为true,传回全部结果资料,in 不管匹配到匹配不到都 全部匹配完毕,使用ex ...
- async/await 的一些知识
博文 Don't Block on Async Code What is the purpose of "return await" in C#? Any difference b ...
- leetcode刷题笔记08 字符串转整数 (atoi)
题目描述 实现 atoi,将字符串转为整数. 在找到第一个非空字符之前,需要移除掉字符串中的空格字符.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字符即 ...
- seaborn使用(绘图函数)
seaborn使用(绘图函数) 数据集分布的可视化 分类数据的绘图 线性关系可视化 一.数据集分布的可视化 distplot kdeplot rugplot 1.distplot() 灵活的绘制单变量 ...
- PHP 字符串变量
PHP 字符串变量 字符串变量用于存储并处理文本. PHP 中的字符串变量 字符串变量用于包含有字符的值. 在创建字符串之后,我们就可以对它进行操作了.您可以直接在函数中使用字符串,或者把它存储在变量 ...
- Docker环境 ELK 快速部署
Docker环境 ELK快速部署 环境 Centos 7.4 , Docker version 17.12 Docker至少3GB内存: #内核配置 echo ' vm.max_map_count = ...
- Android 学习笔记一 自定义按钮背景图
入门学到的一些组件都是比较规矩的,但在实际应用中,我们需要更多特色的组件,例如一个简单的Button,所以我们必须要自定义它的属性. 遇到的问题:用两张图片来代替按钮,分别表示点击前后 解决方法:用I ...
- Dynamics CRM 导出系统中实体的属性字段到EXCEL
我们在CRM中看元数据信息,可以通过SDK中的metadata browser的解决方案包,但该解决方案包只是在可视化上方便了,但如果我们需要在excel中整理系统的数据字典时这个解决方案包就派不上用 ...
- Linux 高性能服务器编程——TCP协议详解
问题聚焦: 本节从如下四个方面讨论TCP协议: TCP头部信息:指定通信的源端端口号.目的端端口号.管理TCP连接,控制两个方向的数据流 TCP状态转移过程:TCP连接的任意一 ...