首页 > 其他 > 详细

345. Reverse Vowels of a String

时间:2019-04-09 17:24:12      阅读:101      评论: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".

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

    }

}

345. Reverse Vowels of a String

原文:https://www.cnblogs.com/MarkLeeBYR/p/10678219.html

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