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 ...
随机推荐
- ASP.NET知识总结(4.请求管道中的19个事件)
(1)BeginRequest: 开始处理请求 (2)AuthenticateRequest授权验证请求,获取用户授权信息 (3):PostAuthenticateRequest获取成功 (4): A ...
- JSON相关基础知识
JSON的定义: 一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性.业内主流技术为其提供了完整的解决方案(有点类似于正则表达式 ,获得了当今大部分语言的支持),从而可以在不同平台间进行数据 ...
- windows2008r2的时间同步小结
一.在windows2008r2域控的环境下进行时间同步的配置(当已经拥有可以使用的ntp服务器,并知晓ip,客户端到其网络正常): 客户端的配置过程如下: 1.搜索窗口输入 gpedit.msc 打 ...
- Android驱动开发5-8章读书笔记
Android驱动开发读书笔记 第五章 S5PV210是一款32位处理器,具有 ...
- 限制Xamarin获取图片的大小
限制Xamarin获取图片的大小在App开发中,经常会使用网络图片.因为这样不仅可以减少App的大小,还可以动态更新图片.但是手机使用网络环境千差万别.当网络环境不是理想的情况下,加载网络图片就是一个 ...
- 如何打开 系统信息窗体和 DirectX诊断工具
开始---运行----msinfo32.exe 运行 位于C:\Program Files\Common Files\Microsoft Shared\Msinfo\msinfo32.exe,显示计算 ...
- CodeForces 515B. Drazil and His Happy Friends
B. Drazil and His Happy Friends time limit per test 2 seconds memory limit per test 256 megabytes in ...
- 简单说说call 与apply
Function.call() 将函数作为对象的方法调用,例如:function.call(thisobj,args,........); thisobj 调用function的对象.在函数主体中, ...
- ZeroMQ接口函数之 :zmq_bind - 绑定一个socket
ZeroMQ 官方地址 : http://api.zeromq.org/4-0:zmq-bind zmq_bind(3) ZMQ Manual - ZMQ/3.2.5 Name zmq_bind - ...
- this, 你到底指向谁?
JS中, this的值到底是什么? 几个月之前, 拜读了<javascript语言精髓>, 里面对于这个问题, 做出了很好的解释... JS中, this的值取决于调用的模式, 而JS中共 ...