LeetCode 第 190 場周賽(前三題)

依舊是第 4 題不做(不會做,也不想花時間搞懂)。

第 1 題:檢查單詞是否爲句中其他單詞的前綴

https://leetcode-cn.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/

Java 代碼:

public class Solution {

    public int isPrefixOfWord(String sentence, String searchWord) {
        String[] splits = sentence.split("\\s");
        int len = splits.length;
        for (int i = 0; i < len; i++) {
            if (splits[i].indexOf(searchWord) == 0) {
                return i + 1;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        String sentence = "hellohello hellohellohello";
        String searchWord = "ell";
        Solution solution = new Solution();
        int res = solution.isPrefixOfWord(sentence, searchWord);
        System.out.println(res);
    }
}

第 2 題:定長子串中元音的最大數目

https://leetcode-cn.com/problems/maximum-number-of-vowels-in-a-substring-of-given-length/

知識點:固定長度的滑動窗口,其實要容易很多,注意一些細節。

Java 代碼:

public class Solution {

    public int maxVowels(String s, int k) {
        char[] charArrayS = s.toCharArray();
        int len = s.length();

        int[] map = new int[128];
        map['a'] = 1;
        map['e'] = 1;
        map['i'] = 1;
        map['o'] = 1;
        map['u'] = 1;

        int count = 0;
        int res = 0;
        for (int i = 0; i < len; i++) {
            if (map[charArrayS[i]] == 1) {
                count++;
            }

            res = Math.max(res, count);
            if (i >= k - 1) {
                // 如果是元音,移除字符
                if (map[charArrayS[i - (k - 1)]] == 1) {
                    count--;
                }
            }

        }
        return res;
    }

    public static void main(String[] args) {
//        String s = "abciiidef";
//        int k = 3;

//        String s = "aeiou";
//        int k = 2;

//        String s = "leetcode";
//        int k = 3;

        String s = "rhythms";
        int k = 4;
        Solution solution = new Solution();
        int res = solution.maxVowels(s, k);
        System.out.println(res);
    }
}

第 3 題:二叉樹中的僞迴文路徑

相關知識點:

  • 「力扣」第 113 題:路徑總和 II
  • 位運算表示迴文,這裏涉及到狀態壓縮的知識點;
  • 還涉及了回溯裏整型狀態的設計。

Java 代碼:

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

    TreeNode(int val) {
        this.val = val;
    }

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

class Solution {

    private int count = 0;

    public int pseudoPalindromicPaths(TreeNode root) {
        dfs(root, 0);
        return count;
    }

    private void dfs(TreeNode node, int status) {
        if (node == null) {
            return;
        }

        status ^= (1 << node.val);

        if (node.left == null && node.right == null) {
            if (status == 0 || (status & (status - 1)) == 0) {
                count++;
            }
            return;
        }

        if (node.left != null) {
            dfs(node.left, status);

        }

        if (node.right != null) {
            dfs(node.right, status);
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章