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);
        }
    }
}

你可能感兴趣的:(力扣)