[Leetcode] Reverse Vowels of a String
2016-09-16 17:45
239 查看
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".
Note:
The vowels does not include the letter "y".
public class Solution {
public String reverseVowels(String s) {
StringBuffer Vowels=new StringBuffer();
StringBuffer reversed=new StringBuffer();
int vowelsCount=0;
for(int i=0;i<s.length();i++)
{
if(s.charAt(i)=='a'||s.charAt(i)=='e'||s.charAt(i)=='i'||s.charAt(i)=='o'||s.charAt(i)=='u'||s.charAt(i)=='A'||s.charAt(i)=='E'||s.charAt(i)=='I'||s.charAt(i)=='O'||s.charAt(i)=='U')
{
Vowels.append(s.charAt(i));
vowelsCount+=1;
}
}
for(int i=0;i<s.length();i++)
{
if(s.charAt(i)=='a'||s.charAt(i)=='e'||s.charAt(i)=='i'||s.charAt(i)=='o'||s.charAt(i)=='u'||s.charAt(i)=='A'||s.charAt(i)=='E'||s.charAt(i)=='I'||s.charAt(i)=='O'||s.charAt(i)=='U')
{
reversed.append(Vowels.toString().charAt(vowelsCount-1));
vowelsCount-=1;
}
else reversed.append(s.charAt(i));
}
return reversed.toString();
}
}
Example 1:
Given s = "hello", return "holle".
Example 2:
Given s = "leetcode", return "leotcede".
Note:
The vowels does not include the letter "y".
public class Solution {
public String reverseVowels(String s) {
StringBuffer Vowels=new StringBuffer();
StringBuffer reversed=new StringBuffer();
int vowelsCount=0;
for(int i=0;i<s.length();i++)
{
if(s.charAt(i)=='a'||s.charAt(i)=='e'||s.charAt(i)=='i'||s.charAt(i)=='o'||s.charAt(i)=='u'||s.charAt(i)=='A'||s.charAt(i)=='E'||s.charAt(i)=='I'||s.charAt(i)=='O'||s.charAt(i)=='U')
{
Vowels.append(s.charAt(i));
vowelsCount+=1;
}
}
for(int i=0;i<s.length();i++)
{
if(s.charAt(i)=='a'||s.charAt(i)=='e'||s.charAt(i)=='i'||s.charAt(i)=='o'||s.charAt(i)=='u'||s.charAt(i)=='A'||s.charAt(i)=='E'||s.charAt(i)=='I'||s.charAt(i)=='O'||s.charAt(i)=='U')
{
reversed.append(Vowels.toString().charAt(vowelsCount-1));
vowelsCount-=1;
}
else reversed.append(s.charAt(i));
}
return reversed.toString();
}
}
相关文章推荐
- 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】345 Reverse Vowels of a String(java)
- [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】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 (in java)
- leetcode 345 Reverse Vowels of a String
- Leetcode Reverse Vowels of a String 345
- 【LeetCode-345】Reverse Vowels of a String
- LeetCode345—Reverse Vowels of a String
- [LeetCode][JavaScript][Python]Reverse Vowels of a String
- Leetcode - Reverse Vowels of a String
- leetcode 345 Reverse Vowels of a String C++