其他分享
首页 > 其他分享> > LeetCode知识点总结 - 345

LeetCode知识点总结 - 345

作者:互联网

LeetCode 168. Excel Sheet Column Title

考点难度
StringEasy
题目

Given a string s, reverse only all the vowels in the string and return it.

The vowels are ‘a’, ‘e’, ‘i’, ‘o’, and ‘u’, and they can appear in both cases.

思路

用两个pointer,现判断是否都是vowel,如果不是就移动到最近的元音,之后swap两个位置上的字母。helper function:判断是否为vowel。

答案
public String reverseVowels(String s) {
        if (s.length() == 0) return s;
        char[] arr = s.toCharArray();
        int low = 0;
        int high = arr.length - 1;
        while (low < high) {
            if (!isVowel(arr[low]) && !isVowel(arr[high])) {
                low++;
                high--;
            } else if (!isVowel(arr[low])) {
                low++;
            } else if (!isVowel(arr[high])) {
                high--;
            } else {
                char temp = arr[low];
                arr[low] = arr[high];
                arr[high] = temp;
                low++;
                high--;
            }
        }
        return new String(arr);
    }

    private boolean isVowel(char ch) {
        ch = Character.toLowerCase(ch);
        return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u';
    }

标签:知识点,arr,ch,return,isVowel,high,345,low,LeetCode
来源: https://blog.csdn.net/m0_59773145/article/details/122008530