Write a function that takes a string as input and reverse only the vowels of a string.

Example 1:
Given s = "hello", return "holle".

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

Example 2:
Given s = "leetcode", return "leotcede".

Note:
The vowels does not include the letter "y".

Solution 1:

class Solution {

    public String reverseVowels(String s) {

        if (s == null || s.length() == 0)

            return s;

        String vowels = "aeiouAEIOU";

        char[] ch = s.toCharArray();

        int start = 0;

        int end = s.length() - 1;

        while (start < end) {

            while (start < end && !vowels.contains(ch[start]+"")) { //这里必须要加start < end,否则在交换之后又会换回来,这里只能是<,不能=,否则后面ch[start]会有越界异常

                start++;             

            }

            while (start < end && !vowels.contains(ch[end]+"")) {//这里必须要加start < end

                end--;             

            }

            char temp = ch[start];

            ch[start] = ch[end];

            ch[end] = temp;

            start++;

            end--; 

        }      

        return new String(ch);

    }

}

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄