原题链接在这里: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:

 1 class Solution {
 2     Character [] vowels = {'A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u'};
 3
 4     public String reverseVowels(String s) {
 5         if(s == null || s.length() == 0){
 6             return s;
 7         }
 8
 9         HashSet<Character> hs = new HashSet<Character>(Arrays.asList(vowels));
10
11         int i = 0;
12         int j = s.length()-1;
13         char [] sArr = s.toCharArray();
14
15         while(i<j){
16             while(i<j && !hs.contains(sArr[i])){
17                 i++;
18             }
19
20             while(i<j && !hs.contains(sArr[j])){
21                 j--;
22             }
23
24             swap(sArr, i, j);
25             i++;
26             j--;
27         }
28
29         return new String(sArr);
30     }
31
32     private void swap(char [] arr, int i, int j){
33         char temp = arr[i];
34         arr[i] = arr[j];
35         arr[j] = temp;
36     }
37 }

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/5812211.html

LeetCode Reverse Vowels of a String相关推荐

  1. LeetCode Reverse Vowels of a String(字符串中元音字符反转)

    题意:给出一个字符串,仅将元音字符反转 思路:设置两点,一个从左开始,一个从右开始,分别找到元音字符,然后替换 代码如下: public class Solution {private boolean ...

  2. Reverse Vowels of a String (反转字符串中的母音)

    leetcode Reverse Vowels of a String 反转字符串中的母音 一.学习要点: 1.find_first_of:查找与字符串str中某个字符相同的位置,并返回他的第一个出现 ...

  3. 345. Reverse Vowels of a String - LeetCode

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

  4. LeetCode345. Reverse Vowels of a String

    345. Reverse Vowels of a String My Submissions QuestionEditorial Solution Total Accepted: 3821 Total ...

  5. 345.反转字符串中的元音字母(Reverse Vowels of a String)

    题目描述 编写一个函数,以字符串作为输入,反转该字符串中的元音字母. 示例 1: 给定 s = "hello", 返回 "holle". 示例 2: 给定 s ...

  6. 从零开始的LC刷题(74): Reverse Vowels of a String

    原题: Write a function that takes a string as input and reverse only the vowels of a string. Example 1 ...

  7. LeetCode:345. Reverse Vowels of a String

    051103 题目 Write a function that takes a string as input and reverse only the vowels of a string. Exa ...

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

  9. C#LeetCode刷题之#345-反转字符串中的元音字母​​​​​​​(Reverse Vowels of a String)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3935 访问. 编写一个函数,以字符串作为输入,反转该字符串中的元 ...

最新文章

  1. 斯坦福CS330 2019秋季课程视频全新上线,专注多任务与元学习
  2. (原) Data Blocks, Extents, and Segments
  3. 200910阶段一C++虚析构
  4. Spring Kafka生产者/消费者样本
  5. 我竟然混进了 Python 高级圈子!
  6. 01-第一章 Java开发中通用的方法和准则
  7. 【论文泛读08】基于深度时空残差网络的城市人群流动预测
  8. 服务器基本安全策略配置
  9. 网页倒计时制作(js)
  10. 360Win10的360wifi无速度问题
  11. Java银行账户管理系统实验总结
  12. Video.js 使用教程 - 手把手教你基于 Vue 搭建 HTML 5 视频播放器
  13. 老马闲评数字化(4)做数字化会不会被供应商拿捏住
  14. coco2d-html5制作弹弓射鸟第一部分---橡皮筋
  15. win10 新版文件资源管理器
  16. java面试题大合集
  17. Spring - Spring容器概念及其初始化过程
  18. 教你 IntelliJ IDEA 永久激活,建议收藏!(转)
  19. 新一轮芯片战的序幕?台积电狂砸两千亿突破2nm
  20. unity基础(8)——3D场景添加音频

热门文章

  1. 1.1 决策树算法原理
  2. 三个案例看Nginx配置安全
  3. 学习ModSecrity Handbook之摘录
  4. 摘录HTTP头部信息的注释
  5. TS基础2(类)-学习笔记
  6. Python使用os.listdir()函数来得目录内容的介绍
  7. 数据结构(三)---双向循环链表的实现---java版
  8. 学习python、数据结构等很多的博客
  9. 结合zxing 和zbar 扫一扫
  10. Linux下切换用户出现su: Authentication failure的解决办法