LeetCode.345. Reverse Vowels of a String

3956 ワード

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”.
public class Solution {
    public String reverseVowels(String s) {
        char[] arr = s.toCharArray();
        int i=0;
        int j=s.length()-1;
        char temp;
        while(i<j){
            boolean ii=false;
            boolean jj=false;
            if(finde(arr[i])) ii=true;
            else i++;
            if(finde(arr[j])) jj=true;
            else j--;
            if(ii==true&&jj==true){
                temp=arr[i];
                arr[i]=arr[j];
                arr[j]=temp;
                i++;j--;
            }
        }

        String str = new String(arr);
        return str;

    }
    private static boolean finde(char arr){
        switch (arr)
        {
        case 'a':return true; 
        case 'e':return true; 
        case 'i':return true; 
        case 'o':return true; 
        case 'u':return true; 
        case 'A':return true; 
        case 'E':return true; 
        case 'I':return true; 
        case 'O':return true;
        case 'U':return true; 
        default : return false;
        }
    }

}