[Leetcode] Reverse Vowels of a String

来源:互联网 发布:网页数据库查询系统 编辑:程序博客网 时间:2024/05/20 06:24

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();    }}

0 0
原创粉丝点击