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 ...
随机推荐
- 【MFC】如何在MFC创建的程序中更改主窗口的属性 与 父窗口 WS_CLIPCHILDREN 样式 对子窗口刷新的影响 与 窗体区域绘制问题WS_CLIPCHILDREN与WS_CLIPSIBLINGS
如何在MFC创建的程序中更改主窗口的属性 摘自:http://blog.sina.com.cn/s/blog_4bebc4830100aq1m.html 在MFC创建的单文档界面中: (基于对话框的, ...
- 深入学习Heritrix---解析Frontier(链接工厂)(转)
深入学习Heritrix---解析Frontier(链接工厂) Frontier是Heritrix最核心的组成部分之一,也是最复杂的组成部分.它主要功能是为处理链接的线程提供URL,并负责链接处理完成 ...
- 简直offer-第四章解决面试题思路(二叉树中和为某一值的路径)
题目:输入一颗二叉树和一个整数,打印出二叉树中节点值的和为输入整数的所有路径.从树的根节点往下一直到叶子节点形成一条路径. 思路:很明显用前序遍历可以从根节点开始遍历到叶子节点,然后将遍历的节点添加到 ...
- 初上dubbo
编译异常 no groovy library is defined for module http://blog.csdn.net/lu_wei_wei/article/details/7601968 ...
- 七、Jmeter + ant + jenkins轻量级接口自动化测试
七.Jmeter + ant + jenkins轻量级接口自动化测试 杀猪不用牛刀,工具没有牛逼高大尚之分,每个工具都有存在的理由:关键是看会不会用,怎么用,有没有用在合适的地方. 需要安装的工具: ...
- (转)c#实现开机自启动
RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersio ...
- 【转】Jmeter做web压力测试时设置持续时间注意点
头一回使用jmeter做web的压力测试,遇到个很莫名其妙的问题,不管我的线程组怎么设置,它就是执行一次就结束了. 设置循环次数为300,不使用调度器--〉执行一次就结束了,循环次数未生效 设置循环次 ...
- NOIP 2008 立体图 (字符串+模拟)
立体图 时间限制: 1 Sec 内存限制: 50 MB提交: 2 解决: 0[提交][状态][讨论版][命题人:外部导入] 题目描述 小渊是个聪明的孩子,他经常会给周围的小朋友们讲些自己认为有趣的 ...
- NOIP2005普及组第3题 采药 (背包问题)
NOIP2005普及组第3题 采药 时间限制: 1 Sec 内存限制: 128 MB提交: 50 解决: 23[提交][状态][讨论版][命题人:外部导入] 题目描述 辰辰是个天资聪颖的孩子,他的 ...
- NOIP2008普及组第3题 传球游戏
NOIP2008普及组第3题 传球游戏 时间限制: 1 Sec 内存限制: 128 MB提交: 29 解决: 16[提交][状态][讨论版][命题人:外部导入] 题目描述 上体育课的时候,小蛮的老 ...