原题链接在这里: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的更多相关文章

  1. [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 ...

  2. 【leetcode】345. Reverse Vowels of a String

    problem 345. Reverse Vowels of a String class Solution { public: string reverseVowels(string s) { , ...

  3. 345. Reverse Vowels of a String - LeetCode

    Question 345. Reverse Vowels of a String Solution 思路:交换元音,第一次遍历,先把出现元音的索引位置记录下来,第二遍遍历元音的索引并替换. Java实 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 【一天一道LeetCode】#345. Reverse Vowels of a String

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...

随机推荐

  1. sqlserver索引小结

    1.1 什么是索引? SQL索引有两种,聚集索引和非聚集索引,索引主要目的是提高了SQL Server系统的性能,加快数据的查询速度与减少系统的响应时间 下面举两个简单的例子: 图书馆的例子:一个图书 ...

  2. SpringMVC学习(一)

    Spring web mvc和Struts2都属于表现层的框架,它是Spring框架的一部分. Web MVC架构 1.用户发起request请求至控制器(Controller) 控制接收用户请求的数 ...

  3. InnoDB VS MyISAM

    首先都是MySql存储引擎.数据库的考虑点一般就是事务(ACID),然后牵扯出的锁机制.如果你需要事务,那就只能选InnoDB了.如果你还需要外键约束,你也只能选择InnoDB.这个是两者最大的区别. ...

  4. 移动端横屏(beta)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. Matlab函数 meshgrid

  6. Java日期处理

    日常工作中经常遇到关于日期的处理,下面把自个写好的Java代码段分享一下,也当做自个的一个备份,同时也欢迎交流,如若分享请注明出处,谢谢. 1.返回两个时间段之间的月份: /** * 返回任意两个月份 ...

  7. 【BZOJ】3561: DZY Loves Math VI

    题意 求\(\sum_{i=1}^{n} \sum_{j=1}^{m} lcm(i, j)^{gcd(i, j)}\)(\(n, m<=500000\)) 分析 很显然要死推莫比乌斯 题解 设\ ...

  8. dev checkbox多选

    GridControl如果要支持多选,设置Options->OptionSeletion->MultiSelet为true就ok.

  9. GregorianCalendar类

    Calendar类实现了公历日历,GregorianCalendar是Calendar类的一个具体实现. Calendar 的getInstance()方法返回一个默认用当前的语言环境和时区初始化的G ...

  10. java并发编程(七)synchronized详解

    Java语言的关键字,当它用来修饰一个方法或者一个代码块的时候,能够保证在同一时刻最多只有一个线程执行该段代码.     一.当两个并发线程访问同一个对象object中的这个synchronized( ...