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:
Given s = "hello", return "holle".
Example 2:
Given s = "leetcode", return "leotcede".
[暴力解法]:抽出来再放回去:不现实
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
[一句话思路]:
直接一个字符串甩过来,String vowels = "aeiouAEIOU";再切碎成数组即可
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
先调指针再交换。和qucik sort的区别:从外围开始,能换就换

[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
直接一个字符串甩过来,String vowels = "aeiouAEIOU";再切碎成数组即可
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
双引号,用来引用字符串,单引号用来表示单个字符。字符+""双引号变成字符串
[关键模板化代码]:
先换指针,再交换值:
while (start < end) {
//adjust start, end
while (start < end && !vowels.contains(chars[start] + "")) {
start++;
}
while (start < end && !vowels.contains(chars[end] + "")) {
end--;
}
//exchange
char temp = chars[start];
chars[start] = chars[end];
chars[end] = temp;
//push to move on
start++;
end--;
}
[其他解法]:
hashset 需要一个个字母地加,不如字符串一刀切
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
class Solution {
public String reverseVowels(String s) {
//cc
if (s == null) {
return s;
}
//ini
String vowels = "aeiouAEIOU";
char[] chars = s.toCharArray();
int start = 0, end = s.length() - 1;
while (start < end) {
//adjust start, end
while (start < end && !vowels.contains(chars[start] + "")) {
start++;
}
while (start < end && !vowels.contains(chars[end] + "")) {
end--;
}
//exchange
char temp = chars[start];
chars[start] = chars[end];
chars[end] = temp;
//push to move on
start++;
end--;
}
//return
return new String(chars);
}
}
345. 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 ...
- 345 Reverse Vowels of a String 反转字符串中的元音字母
编写一个函数,以字符串作为输入,反转该字符串中的元音字母.示例 1:给定 s = "hello", 返回 "holle".示例 2:给定 s = "l ...
- [LeetCode] Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...
- [LeetCode] 151. Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
- [LintCode] Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
- 345. Reverse Vowels of a String - LeetCode
Question 345. Reverse Vowels of a String Solution 思路:交换元音,第一次遍历,先把出现元音的索引位置记录下来,第二遍遍历元音的索引并替换. Java实 ...
- Leetcode 345 Reverse Vowels of a String 字符串处理
题意:倒置字符串中的元音字母. 用两个下标分别指向前后两个相对的元音字母,然后交换. 注意:元音字母是aeiouAEIOU. class Solution { public: bool isVowel ...
- 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 ...
- 【一天一道LeetCode】#345. Reverse Vowels of a String
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...
随机推荐
- 判定客户端IP所在的省市[2]
在上一篇地址控件的基础上,添加了判定客户端IP所在省市的功能. 调用新浪提供的IP库接口:http://counter.sina.com.cn/ip/,(可直接点击链接查看返回的数据) 126提供的接 ...
- 【PS实例】照片拼图的制作
本系列教程将开始讲解PS的一些制作实例,通过实例的讲解同时介绍各种工具和面板机快捷键的使用,这样能够让大家更有兴趣学习,在学习的同时能够创造出自己喜欢的东西.本人使用的教程都是根据本人多次调试制作,仅 ...
- ul li 水平居中
li的float:left方法显然有一个问题,就是无法居中(水平),只能使用padding-left或margin-right的方法方法来固定其居中.但这样可能在宽屏与窄屏的显示不一致.使用这种方法主 ...
- push()、shift()与pop()、unshift()、splice()
1.末端的添加和移除:push()是用来在数组末端添加项,pop()在数组末端移除项: 2.前端的添加和移除:shift()在移除数组的第一个项(前端),unshift()在数组前端添加项: 3.pu ...
- gradle 命令行
1. 帮助 ./gradlew -h 2. gradle 可执行tasks gradle tasks or ./gradlew tasks 3. gradle help 任务 帮助了解每个task 的 ...
- mybatis-generator的坑
有天发现mybatis-generator不能用,要加什么根项目前缀, 搞到我重新从github下载,然后发现仓库用不了,原来新项目会改我的maven配置(新坑 后来终于明白要在子项目栏使用手脚架,就 ...
- php中mb_strlen,mb_substr根据中文长度截取字符串
大于8截取,小于等于则不截取. 结合thinkphp模板引擎规则,代码如下: <,,'utf-8'}..<else/>{sh:$vo.name}</if> 这里if中的函 ...
- Oracle 11g r2 rac +openfiler 2.99 安装
1 openfiler 2.99 安装 在官网下载iso文件,这里选择openfileresa-2.99.1-x86_64-disc1.iso 版本,在vbox下创建一个虚拟机 --vbox 选择li ...
- (转).Net基础体系和跨框架开发普及
在园子里看到了一篇关于.net体系及框架开发的文章,感触颇深,身为一个.net程序员,发现自己在这方面的跟进和理解远远不够.转到自己这里,分享的同时方便日后查看. 原文链接: http://www.c ...
- python学习(十七) 扩展python
c, c++, java比python快几个数量级. 17.1 考虑哪个更重要 开发速度还是运行速度更重要. 17.2 非常简单的途径:Jython和IronPython Jython可以直接访问JA ...