LeetCode Reverse Vowels of a String
原题链接在这里:https://leetcode.com/problems/reverse-vowels-of-a-string/
题目:
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".
题解:
典型的two pointers, 到了vowel时对调.
Note: 对调完要再移动双指针.
若用到Array.asList(arr), arr 这里要用Character型,而不是char型.
Time Complexity: O(s.length()). Space:O(s.length()), 因为建立了char array.
AC Java:
class Solution {
Character [] vowels = {'A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u'};
public String reverseVowels(String s) {
if(s == null || s.length() == 0){
return s;
}
HashSet<Character> hs = new HashSet<Character>(Arrays.asList(vowels));
int i = 0;
int j = s.length()-1;
char [] sArr = s.toCharArray();
while(i<j){
while(i<j && !hs.contains(sArr[i])){
i++;
}
while(i<j && !hs.contains(sArr[j])){
j--;
}
swap(sArr, i, j);
i++;
j--;
}
return new String(sArr);
}
private void swap(char [] arr, int i, int j){
char temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
LeetCode Reverse Vowels of a String的更多相关文章
- [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 ...
- 【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(C++)
345. Reverse Vowels of a String Write a function that takes a string as input and reverse only the v ...
- 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 Words in a String:Evaluate Reverse Polish Notation
LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithm ...
- Python [Leetcode 345]Reverse Vowels of a String
题目描述: Write a function that takes a string as input and reverse only the vowels of a string. Example ...
- 【一天一道LeetCode】#345. Reverse Vowels of a String
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...
随机推荐
- AE开发中对GDB以及shapefile的读取、对FeatureClass的相关操作
读取gdb方法 private void btn_Click(object sender, EventArgs e) { FolderBrowserDialog dlg = new FolderBro ...
- redis 密码配置
http://blog.csdn.net/vtopqx/article/details/46833099 http://www.2cto.com/database/201412/365757.html ...
- 安装SQL SERVER 2005出现“sql2005 执行安装向导期间出错 ”
安装sql server 2005时出现“sql2005 执行安装向导期间出错”的提示,百度找了一下,发现原来是解压时候才安装了CD1的,还有CD2的没解压安装,解压CD2安装即可
- 吃透Javascript数组操作的正确姿势—再读《Js高程》
Javascript中关于数组对象的操作方法比较多也比较杂,正好再次捡起<Javascript高级程序设计>来读,把它们一一总结梳理了一下: 方法类别 方法名称 方法描述 参数 返回值 备 ...
- HDU 2509 Be the Winner nim博弈变形
Be the Winner Problem Description Let's consider m apples divided into n groups. Each group contai ...
- swiper框架,实现轮播图等滑动效果
http://www.swiper.com.cn/ 做个记录而已,这个没什么好说的,对于需要手机端开发实现触摸等方式可以看看.
- Nginx二级域名及多Server反向代理配置
Nginx强大的正则表达式支持,可以使server_name的配置变得很灵活,如果你要做多用户博客,那么每个用户拥有自己的二级域名也就很容易实现了. 注:nginx反向代理同一ip多个域名,给head ...
- 分布式缓存技术redis学习系列(三)——redis高级应用(主从、事务与锁、持久化)
上文<详细讲解redis数据结构(内存模型)以及常用命令>介绍了redis的数据类型以及常用命令,本文我们来学习下redis的一些高级特性. 安全性设置 设置客户端操作秘密 redis安装 ...
- node模块函数图解
已截图方式记录模块信息: HTTP模块: 对于网络返回处理状态封装了很多种,我已截图展现 以上状态也是在http协议中包含的状态. http函数: path模块:
- 清华学堂 列车调度(Train)
列车调度(Train) Description Figure 1 shows the structure of a station for train dispatching. Figure 1 In ...