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:
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相关推荐
- LeetCode Reverse Vowels of a String(字符串中元音字符反转)
题意:给出一个字符串,仅将元音字符反转 思路:设置两点,一个从左开始,一个从右开始,分别找到元音字符,然后替换 代码如下: public class Solution {private boolean ...
- Reverse Vowels of a String (反转字符串中的母音)
leetcode Reverse Vowels of a String 反转字符串中的母音 一.学习要点: 1.find_first_of:查找与字符串str中某个字符相同的位置,并返回他的第一个出现 ...
- 345. Reverse Vowels of a String - LeetCode
Question 345. Reverse Vowels of a String Solution 思路:交换元音,第一次遍历,先把出现元音的索引位置记录下来,第二遍遍历元音的索引并替换. Java实 ...
- LeetCode345. Reverse Vowels of a String
345. Reverse Vowels of a String My Submissions QuestionEditorial Solution Total Accepted: 3821 Total ...
- 345.反转字符串中的元音字母(Reverse Vowels of a String)
题目描述 编写一个函数,以字符串作为输入,反转该字符串中的元音字母. 示例 1: 给定 s = "hello", 返回 "holle". 示例 2: 给定 s ...
- 从零开始的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 ...
- 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 ...
- 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 ...
- C#LeetCode刷题之#345-反转字符串中的元音字母(Reverse Vowels of a String)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3935 访问. 编写一个函数,以字符串作为输入,反转该字符串中的元 ...
最新文章
- 斯坦福CS330 2019秋季课程视频全新上线,专注多任务与元学习
- (原) Data Blocks, Extents, and Segments
- 200910阶段一C++虚析构
- Spring Kafka生产者/消费者样本
- 我竟然混进了 Python 高级圈子!
- 01-第一章 Java开发中通用的方法和准则
- 【论文泛读08】基于深度时空残差网络的城市人群流动预测
- 服务器基本安全策略配置
- 网页倒计时制作(js)
- 360Win10的360wifi无速度问题
- Java银行账户管理系统实验总结
- Video.js 使用教程 - 手把手教你基于 Vue 搭建 HTML 5 视频播放器
- 老马闲评数字化(4)做数字化会不会被供应商拿捏住
- coco2d-html5制作弹弓射鸟第一部分---橡皮筋
- win10 新版文件资源管理器
- java面试题大合集
- Spring - Spring容器概念及其初始化过程
- 教你 IntelliJ IDEA 永久激活,建议收藏!(转)
- 新一轮芯片战的序幕?台积电狂砸两千亿突破2nm
- unity基础(8)——3D场景添加音频