• 345. Reverse Vowels of a String


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

    Example 1:

    Input: "hello"
    Output: "holle"
    

    Example 2:

    Input: "leetcode"
    Output: "leotcede"

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

    class Solution {
        public String reverseVowels(String s) {
            char[] c = s.toCharArray();
            List<Character> list = new ArrayList();
            list.add('a');
            list.add('e');
            list.add('i');
            list.add('o');
            list.add('u');
            list.add('A');
            list.add('E');
            list.add('I');
            list.add('O');
            list.add('U');
            String res = "";
            if(s.length() == 0) return res;
            int st = 0, end = s.length() - 1;
            while(st < end){
                char t1 = c[st];
                char t2 = c[end];
                if(list.contains(t1) && list.contains(t2)){
                    char tmp = c[st];
                    c[st] = c[end];
                    c[end] = tmp;
                    st++;
                    end--;
                }
                else if(list.contains(t1) && !list.contains(t2)) end--;
                else if(!list.contains(t1) && list.contains(t2)) st++;
                else{
                    end--;
                    st++;
                }
            }
            return(String.valueOf(c));
        }
    }
    class Solution {
        public String reverseVowels(String s) {
            char[] c = s.toCharArray();
            String ind = "aeiouAEIOU";
            String res = "";
            if(s.length() == 0) return res;
            int st = 0, end = s.length() - 1;
            while(st < end){
                char t1 = c[st];
                char t2 = c[end];
                if(ind.indexOf(t1)!=-1 && ind.indexOf(t2)!=-1){
                    char tmp = c[st];
                    c[st] = c[end];
                    c[end] = tmp;
                    st++;
                    end--;
                }
                else if(ind.indexOf(t1)!=-1 && ind.indexOf(t2) == -1) end--;
                else if(ind.indexOf(t1) == -1 && ind.indexOf(t2)!=-1) st++;
                else{
                    end--;
                    st++;
                }
            }
            return(String.valueOf(c));
        }
    }
  • 相关阅读:
    os 模块删除图片操作
    python base64基本使用
    django 跨域
    git 回滚
    多线程
    原生sql子查询 和psql 子查询
    Python 3.x 中"HTTP Error 403: Forbidden"问题的解决方案
    报错(AttributeError: 'str' object has no attribute 'items')的解决办法
    C++虚函数
    C++泛型程序设计和多态
  • 原文地址:https://www.cnblogs.com/wentiliangkaihua/p/11824431.html
Copyright © 2020-2023  润新知