leetcode :Reverse Vowels of a String
来源:互联网 发布:dnf数据异常会封号吗 编辑:程序博客网 时间:2024/05/17 22:55
原题:
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".
分析:返回一个新的字符串,其中元音字符排序是原字符串中元音字符的逆序,其他字符顺序不变。
题目EASY,直接上代码:
public class Solution {
public String reverseVowels(String s) {
StringBuffer sb = new StringBuffer();
List<Character> vowel =new ArrayList<Character>();
vowel.add('a');
vowel.add('e');
vowel.add('i');
vowel.add('o');
vowel.add('u');
vowel.add('A');
vowel.add('E');
vowel.add('I');
vowel.add('O');
vowel.add('U');
List<Character> ls =new ArrayList<Character>();
for (int i = 0; i < s.length(); i++) {//将s中所有元音字符放入集合
if(vowel.contains(s.charAt(i)))
ls.add(s.charAt(i));
}
for (int i = 0; i < s.length(); i++) {
if(!vowel.contains(s.charAt(i)))
sb.append(s.charAt(i));//非元音字符直接拼接成字符串
else
sb.append(ls.remove((ls.size()-1)));//反向拼接元音字符
}
return sb.toString();
}
}
- Leetcode Reverse Vowels of a String 345
- LeetCode 345. Reverse Vowels of a String
- LeetCode - Reverse Vowels of a String
- leetcode-345. Reverse Vowels of a String
- 【LeetCode-345】Reverse Vowels of a String
- #leetcode#345. Reverse Vowels of a String
- leetcode 345Reverse vowels of a string
- [leetcode] 345. Reverse Vowels of a String
- Leetcode:345. Reverse Vowels of a String
- LeetCode *** 345. Reverse Vowels of a String
- leetcode 345:Reverse Vowels of a String
- leetcode笔记:Reverse Vowels of a String
- LeetCode 345 Reverse Vowels of a String
- LeetCode:Reverse Vowels of a String
- LeetCode.345. Reverse Vowels of a String
- LeetCode 345 Reverse Vowels of a String
- Leetcode - Reverse Vowels of a String
- LeetCode 345. Reverse Vowels of a String
- Jfinal 集成spring 、cxf 做webService服务
- zcuSHbuD2Wn3.exe
- 数的存储方式
- 开发Android遇到过的坑
- uint8_t、uint16_t、uint32_t、uint64_t、size_t、ssize_t、
- leetcode :Reverse Vowels of a String
- 夏令营第一题
- NPM 使用介绍
- POJ1703-Find them, Catch them
- android隐藏虚拟键盘的代码参考
- Node.js REPL(交互式解释器)
- hdu_5110_Alexandra and COS(DP+分块思想)
- 火爆的口袋妖怪是如何开发出来的?我们带你去了解下它的开发商
- Node.js 回调函数