首页 > 其他 > 详细

Reverse Vowels of a String

时间:2016-07-24 06:59:33      阅读:160      评论:0      收藏:0      [点我收藏+]

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".

 

vowels:元音aeiou

 

解法:two pointers

public class Solution {
    public String reverseVowels(String s) {
        String vowel = "aeiouAEIOU";
        int start = 0;
        int end = s.length()-1;
        char[] chars = s.toCharArray();
        while(start<end)
        {
            while(start<end&&!vowel.contains(chars[start]+""))
            {
                start++;
            }
            while(start<end&&!vowel.contains(chars[end]+""))
            {
                end--;
            }
            
            char temp = chars[start];
            chars[start]=chars[end];
            chars[end]=temp;
            start++;
            end--;
        }
        
        return String.valueOf(chars);
        
    }
}
chars[end]+"" 这里加上“”很重要,不然会出以下错误:
error: incompatible types: char cannot be converted to CharSequence

 

Reverse Vowels of a String

原文:http://www.cnblogs.com/hygeia/p/5700019.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!