原题链接在这里: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. 打开APK里的AndroidManifest.xml乱码

    直接解压apk,打开AndroidManifest.xml显示乱码,因为这里面是二进制字符,和打开文件的编辑器无关.(也可以用ultraedit打开查看,有明文显示.只是看起来搜起来不是很方便而已) ...

  2. 使用tomact监控应用服务器的性能

    第一步:在D:\apache-tomcat-7.0.63\conf 配置目录找到tomcat-users.xml 打开添加用户角色及权限 第二步:重启tomact 第三步:浏览器上打开http://1 ...

  3. CozyRSS开发记录6-继续补全订阅内容栏

    CozyRSS开发记录6-继续补全订阅内容栏 1.订阅内容栏布局 按照之前的原型图,把订阅内容栏分成三块.Xaml如下: 2.照葫芦画瓢,完成头部和列表 头部依然使用ColorZone,右侧再放两个按 ...

  4. opencv2 使用鼠标绘制矩形并截取和保存矩形区域图像

    前言 好长时间没写博文了,今天偷偷懒写篇关于opencv2中鼠标响应操作的文章. 鼠标操作属于用户接口设计,以前一直使用Qt来做,但是如果只需要简单的鼠标,键盘操作,直接调用opencv库的函数也未尝 ...

  5. Codeforces Round #222 (Div. 1) D. Developing Game 线段树有效区间合并

    D. Developing Game   Pavel is going to make a game of his dream. However, he knows that he can't mak ...

  6. one_person年轻的程序员

    回顾大学三年,通过良师的教导和自身的刻苦学习,我已初步掌握如何运用计算机编程,也养成了认真对待学习和工作的好习惯! 在思想品德上,本人有良好道德修养,并有坚定的政治方向.我热爱祖国,热爱人民,遵纪守法 ...

  7. Javascript并发模型和事件循环

    Javascript并发模型和事件循环 JavaScript的"并发模型"是基于事件循环的,这个并发模型有别于Java的多线程, javascript的并发是单线程的. Javas ...

  8. 在freemarker中,价格 怎么将¥100变成 ¥100.00

    ${tempNum?string.currency}或${tempNum?string(“currency”)} à结果为¥20.00${tempNum?string. percent}或${temp ...

  9. 如何记录搜索引擎爬行记录php版

    写博客也有一段时间了,为什么搜索引擎迟迟不收录你的页面呢?想知道每天都有哪些蜘蛛“拜访”你的网站吗?作为一名网站长,有必要知道每天都有哪些蜘蛛爬行过你的网站,以便于了解各搜索引擎蜘蛛爬行频率,对网站进 ...

  10. [html] 有利于seo优化的div+css命名规范

    搜索引擎优化(seo)对命名规范有很多要求,下面是我收集的一些当下主流的命名(还是比较常用的): CSS样式命名 说明 网页公共命名 #wrapper 页面外围控制整体布局宽度 #container或 ...