Leetcode 345 Reverse Vowels in a String
两个for
第一个for将每一个元音依次存放进一个char数组
第二个for,每检测到元音,就从char数尾部开始,依次赋值
如何检测元音呢?当然写一个冗长的if(),不过我们有更好的选择
hashset的contains,
或者String自带的contains,
或者建一个int[128],因为元音有5个,算上大小写,一共10个,他们的ascii值都在128以内,然后将元音对应的int[]值设为1,其它设为0,只要检测int[strs[i]] ?= 0即可判断是否为元音
class Solution {
public String reverseVowels(String s) {
if(s.length() == 0)
return "";
String v = "aeiouAEIOU";
char[] vowel = new char[s.length()];
char[] s2 = s.toCharArray();
int idx = 0;
for(int i=0; i<s.length(); i++){
if(v.contains(s.charAt(i)+""))
vowel[idx++] = s.charAt(i);
}
for(int i=0; i<s.length(); i++){
if(v.contains(s.charAt(i)+""))
s2[i] = vowel[--idx];
}
return new String(s2);
}
}
Leetcode 345 Reverse Vowels in a String的更多相关文章
- 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
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 字符串处理
题意:倒置字符串中的元音字母. 用两个下标分别指向前后两个相对的元音字母,然后交换. 注意:元音字母是aeiouAEIOU. class Solution { public: bool isVowel ...
- LeetCode 345. Reverse Vowels of a String(双指针)
题意:给定一个字符串,反转字符串中的元音字母. 例如: Input: "leetcode" Output: "leotcede" 法一:双指针 class So ...
- 【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
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...
随机推荐
- 关于Modelsim安装闪退
在盗版Windows系统上,安装Modelsim时可能出现闪退. 现象表现为,在任务管理器中仍然有Modelsim的进程,但是看不到安装界面. 碰到这种情况可以尝试如下方法:退到安全模式下安装. 一般 ...
- VS2010-MFC(对话框:一般属性页对话框的创建及显示)
转自:http://www.jizhuomi.com/software/169.html 属性页对话框包括向导对话框和一般属性页对话框两类,上一节演示了如何创建并显示向导对话框,本节将继续介绍一般属性 ...
- 763 Hex Conversion
原题网址:http://www.lintcode.com/zh-cn/problem/hex-conversion/ Given a decimal number n and an integer k ...
- Neo4j全文检索
全文检索基本概念 搜索 搜索这个行为是用户与搜索引擎的一次交互过程,用户需要找一些数据,他提供给搜索引擎一些约束条件.搜索引擎通过约束条件抽取一些结果给用户 搜索引擎 搜索引擎存在的目的是存储,查找和 ...
- 封装一个C#日志类Loger
public class Loger { /// <summary> /// 写入日志 /// </summary> /// <param name="cont ...
- ps axu 参数说明
问题:1.ps axu 看到进程的time不清楚什么意思 ru: resin 31507 0.2 1.3 3569452 98340 ? Sl Jul28 7:11 / ...
- 嘴巴题9 Codeforces 453A. Little Pony and Expected Maximum
A. Little Pony and Expected Maximum time limit per test 1 second memory limit per test 256 megabytes ...
- 基于windows消息的响应USB插入或取出
导语:当有设备进入windows时,系统会向所有的应用层发送WM_DEVICECHANGE消息.进一步根据相应的事件判断设备. LRESULT CALLBACK WndProc(HWND hWnd, ...
- Ansible-playbook简单应用的几个实例
①ansible-playbook的循环: 重复执行某任务:对迭代项的引用,固定变量名为“item”,而后要在task中使用with_items给定要迭代的元素列表,列表方法:字符串/字典(类似jso ...
- Nginx反向代理Odoo并转为https
生成证书文件 生成自签名证书,并放在指定位置 $ openssl req -x509 -days 3650 -subj '/CN=odoo.youyun.com/' -nodes -newkey rs ...