zoukankan      html  css  js  c++  java
  • leetcode 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:
    Given s = "hello", return "holle".

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

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

    public class Solution {
        public String reverseVowels(String s) {
            if(s == "" || s.length() == 0) return "";
            
            int i = 0;
            int j = s.length() - 1;
            
            String vowels = "aeiouAEIOU";
            char[] arr = s.toCharArray();
     
            while(i < j ){
                while(i < j && !vowels.contains(arr[i]+"")){
                    i++;
                }
                
                while(i < j && !vowels.contains(arr[j]+"")){
                    j--;
                }
                
                char temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
                i++;
                j--;
            }
            return new String(arr);
        }
        
    }
  • 相关阅读:
    HttpClient
    Android子线程访问网络
    PhoneURLConnectGEt
    PhoneHttpGet
    PhoneNote
    SQLite
    书单
    通过Web预测网页出版日期的学习
    LeetCode-Maximum Subarray[dp]
    LeetCode-Triangle[dp]
  • 原文地址:https://www.cnblogs.com/iwangzheng/p/5707442.html
Copyright © 2011-2022 走看看