您的位置:首页 > 其它

[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();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: