当前位置:网站首页>Leetcode- reverse vowels in string - simple

Leetcode- reverse vowels in string - simple

2022-06-13 05:47:00 AnWenRen

title :345 Reverse vowels in a string - Simple

subject

Give you a string s , Invert only all vowels in the string , And return the result string .

Vowels include 'a''e''i''o''u', And may appear in both case .

Example 1

 Input :s = "hello"
 Output :"holle"

Example 2

 Input :s = "leetcode"
 Output :"leotcede"

Tips

  • 1 <= s.length <= 3 * 105
  • s from Printable ASCII Character composition

Code Java

public String reverseVowels(String s) {
    
    int start = 0;
    int end = s.length()-1;
    char[] str = s.toCharArray();
    while (start < end) {
    
        while(start < str.length){
    
            char x = str[start];
            if (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u'
                    || x == 'A' || x == 'E' || x == 'I' || x == 'O' || x == 'U') {
    
                break;
            }
            start ++;
        }
        while(end >= 0){
    
            char x = str[end];
            if (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u'
                    || x == 'A' || x == 'E' || x == 'I' || x == 'O' || x == 'U') {
    
                break;
            }
            end --;
        }
        if (start < end){
    
            char temp = str[start];
            str[start] = str[end];
            str[end] = temp;
            start ++;
            end --;
        } else break;
    }
    return new String(str);
}
原网站

版权声明
本文为[AnWenRen]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280507176526.html