345. 反转字符串中的元音字母(java)

编写一个函数,以字符串作为输入,反转该字符串中的元音字母。

示例 1:

输入: "hello"
输出: "holle"
示例 2:

输入: "leetcode"
输出: "leotcede"
说明:
元音字母不包含字母"y"。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/reverse-vowels-of-a-string
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
class Solution {
    public String reverseVowels(String s) {
        if(s == null) return null;
        char[] str = s.toCharArray();
        int i = 0;
        int j = s.length()-1;
        char tmp = 'a';
        int mark = 0;
        while(i <= j){
            if(mark == 0){
                if(str[i] == 'a'||str[i] == 'e'||str[i] == 'i'||str[i] == 'o'||str[i] == 'u'||str[i] == 'A'||str[i] == 'E'||str[i] == 'I'||str[i] == 'O'||str[i] == 'U'){
                    mark = 1;
                    tmp = str[i];
                } 
                i++;
            }
            else {
                if(str[j] == 'a'||str[j] == 'e'||str[j] == 'i'||str[j] == 'o'||str[j] == 'u'||str[j] == 'A'||str[j] == 'E'||str[j] == 'I'||str[j] == 'O'||str[j] == 'U'){
                    mark = 0;
                    str[i-1] = str[j];
                    str[j] = tmp;
                } 
                j--;                
            }
        }
        return String.valueOf(str);
    }
}
发布了136 篇原创文章 · 获赞 19 · 访问量 8051

猜你喜欢

转载自blog.csdn.net/weixin_43306331/article/details/103965936