leetcode 345. inversion vowels (Java double pointer) (where there do not know) the string

https://leetcode-cn.com/problems/reverse-vowels-of-a-string/submissions/

 

Define a boolean array, so that all vowels (case sensitive, aeiouAEIOU) where ascii code is true, the other is false, turned into the string s (toCharArray () function), two-hand turn determines encountered vowel is exchanged.

 

class Solution {
     private  static  boolean [] Vowels = new new  boolean [123 ]; // first at the question, why should private and static modification?
    static { 
        Vowels [ 'A'] = to true ; 
        Vowels [ 'E'] = to true ; 
        Vowels [ 'I'] = to true ; 
        Vowels [ 'O'] = to true ; 
        Vowels [ 'U'] = to true ; 
        Vowels [ ' A '] = to true ; 
        Vowels [ ' E '] = to true ; 
        Vowels ['I']=true;
        vowels['O']=true;
        vowels['U']=true;
    }
    
    public String reverseVowels(String s) {
        char[] ans=s.toCharArray();
        int i=0,j=ans.length-1;
        while(i<j){
            if(!vowels[ans[i]])
            {
                i++;
                continue;
            }
            if(!vowels[ans[j]])
            {
                j-; Continue ; 
            } 
            char TEMP = ANS [I]; 
            ANS [I] = ANS [J]; 
            ANS [J] = TEMP; 
            I ++ ; 
            J - ; 
        } 
        return  new new String (ANS); //? This form do not understand, () Why can directly fill in ANS 
    } 
}

 

Guess you like

Origin www.cnblogs.com/y1040511302/p/11432184.html