345. Reverse Vowels of a String
来源:互联网 发布:卸载office2010的软件 编辑:程序博客网 时间:2024/06/15 20:58
Write a function that takes a string asinput and reverse only the vowels of a string.
翻译:编写一个函数,它接受一个字符串作为输入,只反转一个字符串的元音。
分析:题目刚开始都没看懂英文,翻译过来才知道。元音是“aiueoAIUEO”,很好想,用快速排序的的做法一样,从两端遍历,遇到元音判断,然后交换。我有两点需要注意:
1、String是final类型,对于final修饰的类型,若是引用类型则指向类型对象不可变,基本类型,则值不能变,意味着是常量,而且使用之前必须初始化。所以用replace不会改变它的值
2、写完后,看了别人的做法,发现String有contains方法。
代码如下:
public class Solution {
public String reverseVowels(String s) {
charch[]=s.toCharArray();
Set<Character>set=new HashSet();
set.add('a');
set.add('i');
set.add('u');
set.add('e');
set.add('o');
set.add('A');
set.add('I');
set.add('U');
set.add('E');
set.add('O');
inti=0,j=s.length()-1;
while(i<j){
while(i<j&&!set.contains(ch[i]))i++;
while(i<j&&!set.contains(ch[j]))j--;
if(i<j){
chartemp=ch[i];
ch[i]=ch[j];
ch[j]=temp;
i++;
j--;
}
}
return new String(ch);
}
}
- String:345. Reverse Vowels of a String
- 345. Reverse Vowels of a String 【E】
- LeetCode 345. Reverse Vowels of a String
- 345. Reverse Vowels of a String
- leetcode-345. Reverse Vowels of a String
- 345. Reverse Vowels of a String
- #leetcode#345. Reverse Vowels of a String
- [leetcode] 345. Reverse Vowels of a String
- 345. Reverse Vowels of a String
- Leetcode:345. Reverse Vowels of a String
- LeetCode *** 345. Reverse Vowels of a String
- 345. Reverse Vowels of a String
- 345. Reverse Vowels of a String
- 345. Reverse Vowels of a String
- 345. Reverse Vowels of a String
- LeetCode.345. Reverse Vowels of a String
- 345. Reverse Vowels of a String
- LeetCode 345. Reverse Vowels of a String
- 基于TCP的QQ聊天工具
- FishC笔记—34 讲 丰富的else语句及简的with语句
- 基于物理的渲染技术(PBR)系列一
- 一点心得体会
- tcp简易聊天程序
- 345. Reverse Vowels of a String
- Linux上源码编译安装MySQL数据库
- Android Studio 如何关联源码(关联你想要的任何版本的源码)
- Two Sum
- 11. Container With Most Water
- 在Android中应用MVP模式
- leetcode#94
- 【git】-git的特点
- 分辨服务器是linux系统还是Windows系统