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 ...
随机推荐
- mysql 分表
1.分表,即把一个很大的表达数据分到几个表中,这样每个表数据都不多. 优点:提高并发量,减小锁的粒度 缺点:代码维护成本高,相关sql都需要改动 2.分区,所有的数据还在一个表中,但物理存储数据根据一 ...
- php面试题2
php面试题及答案(原创)收藏 基础题: 1.表单中 get与post提交方法的区别? 答:get是发送请求HTTP协议通过url参数传递进行接收,而post是实体数据,可以通过表单提交大量信息. 2 ...
- PHP的命名空间 与类是自动加载
namespace 假设如果不使用namespace,那么每个类在一个项目中的名字就必须是固定的.因为php在new的时候不管是调用autoload还是调用已加载过的类,都存在一个类名对应的文件.所以 ...
- C++ 的二进制语法与语义
/* 转载请注明出处:http://www.cnblogs.com/Martinium/p/binary_literal.html */ 二进制的语法 C/C++ 默认数字使用十进制,八进制使用前缀 ...
- 【Java EE 学习 25 下】【网上图书商城js小技术点总结】
1.日历控件的使用 日历控件源代码: /** * add auto hide when mouse moveout * * @version 1.0.1 * @date 2010-11-23 * @a ...
- 【Cocos2d-x for WP8 学习整理】(2)Cocos2d-Html5 游戏 《Fruit Attack》 WP8移植版 开源
这一阵花了些时间,把 cocos2d-html5 里的sample 游戏<Fruit Attack>给移植到了WP8上来,目前已经实现了基本的功能,但是还有几个已知的bug,比如WP8只支 ...
- Structs框架
一.准备工作及实例 1.解压struts-2.1.6-all.zip(structs网上下载) apps目录:struts2自带的例子程序 docs目录:官方文档. lib 目录:存放所有jar文件. ...
- Tomcat 解压版安装
1.下载tomcat7.0 http://tomcat.apache.org/download-70.cgi
- MySQL生成模型
根据数据库表生成Model using System; using System.Collections.Generic; using System.Data; using System.Text; ...
- 用Struts2拦截器实现文件下载前的验证
思想:用户登录后,将登录信息存储在session中,每次需要验证时,取出来验证 缺陷:没有实现多用户登录时的情况 实行步骤: 登录信息的存储: ActionContext actionContext ...