zoukankan      html  css  js  c++  java
  • LeetCode-Reverse Vowels of a String

    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:

    public class Solution {
        public String reverseVowels(String s) {
            if (s.length()<=1) return s;
            
            HashSet<Character> vowels = new HashSet<Character>();
            vowels.add('a');
            vowels.add('e');
            vowels.add('i');
            vowels.add('o');
            vowels.add('u');
            vowels.add('A');
            vowels.add('E');
            vowels.add('I');
            vowels.add('O');
            vowels.add('U');
            
            char[] sArr = s.toCharArray();
            int p1 = 0, p2=s.length()-1;
            while (p1<p2){
                while (p1<p2 && !vowels.contains(sArr[p1])) p1++;
                while (p1<p2 && !vowels.contains(sArr[p2])) p2--;
                
                char temp = sArr[p1];
                sArr[p1++] = sArr[p2];
                sArr[p2--] = temp;
            }
            
            return new String(sArr);
        }
    }
  • 相关阅读:
    SQL server查询笔记
    thinkphp ajax无刷新上传头像
    JSTL
    EL表达式
    jBox 弹框内容交互
    网页可读不可写
    readonly与disable的区别
    vuex 的使用
    页面向上滚动
    按照数组中的对象属性进行排序
  • 原文地址:https://www.cnblogs.com/lishiblog/p/5870506.html
Copyright © 2011-2022 走看看