首页 > 其他分享 >[Leetcode Weekly Contest]311

[Leetcode Weekly Contest]311

时间:2022-09-19 20:12:08浏览次数:112  
标签:TreeNode cur val Contest int 311 节点 字符串 Leetcode

链接:LeetCode

[Leetcode]2413. 最小偶倍数

给你一个正整数 n ,返回 2 和 n 的最小公倍数(正整数)。

根据题意,当 n 为奇数时,答案为 2n,当 n 为偶数时,答案为 n。

class Solution {
    public int smallestEvenMultiple(int n) {
        if((n & 1) == 0) return n;
        else return n<<1;
    }
}

[Leetcode]2414. 最长的字母序连续子字符串的长度

字母序连续字符串 是由字母表中连续字母组成的字符串。换句话说,字符串 "abcdefghijklmnopqrstuvwxyz" 的任意子字符串都是 字母序连续字符串 。
例如,"abc" 是一个字母序连续字符串,而 "acb" 和 "za" 不是。
给你一个仅由小写英文字母组成的字符串 s ,返回其 最长 的 字母序连续子字符串 的长度。

遍历即可。

class Solution {
    public int longestContinuousSubstring(String s) {
        int res = 1;
        int cur = 1;
        for(int i=1;i<s.length();++i) {
            if(s.charAt(i) == (char)(s.charAt(i-1)+1)) {
                cur ++;
                res = Math.max(res, cur);
            }
            else cur = 1;
        }
        return res;
    }
}

[Leetcode]2415. 反转二叉树的奇数层

给你一棵 完美 二叉树的根节点 root ,请你反转这棵树中每个 奇数 层的节点值。
例如,假设第 3 层的节点值是 [2,1,3,4,7,11,29,18] ,那么反转后它应该变成 [18,29,11,7,4,3,1,2] 。
反转后,返回树的根节点。
完美 二叉树需满足:二叉树的所有父节点都有两个子节点,且所有叶子节点都在同一层。
节点的 层数 等于该节点到根节点之间的边数。

BFS或者DFS。
BFS 这棵树,对于奇数层,直接交换层里面的所有元素值(交换的是元素值,不是节点)。DFS依然是交换值的思路,通过同时递归左右子树实现。

/**
 * Definition for a binary tree node.
 * public 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 {
    public TreeNode reverseOddLevels(TreeNode root) {
        dfs(root.left, root.right, true);
        return root;
        // return bfs(root);
    }

    // DFS
    public void dfs(TreeNode node1, TreeNode node2, boolean isOdd) {
        if(node1 == null) return;
        if(isOdd) {
            node1.val = node1.val ^ node2.val;
            node2.val = node1.val ^ node2.val;
            node1.val = node1.val ^ node2.val;
        }
        dfs(node1.left, node2.right, !isOdd);
        dfs(node1.right, node2.left, !isOdd);
    }

    // BFS
    public TreeNode bfs(TreeNode root) {
        Deque<TreeNode> q = new ArrayDeque<>();
        q.offer(root);
        boolean isOdd = false;
        while(!q.isEmpty()) {
            ArrayList<TreeNode> list = new ArrayList<>();
            int n = q.size();
            // Note that q.size() will increase when offered
            for(int i=0;i<n;++i) {
                TreeNode node = q.poll();
                if(node.left != null) {
                    q.offer(node.left);
                    q.offer(node.right);
                    list.add(node.left);
                    list.add(node.right);
                }
            }
            if(!isOdd) swap(list);
            isOdd = !isOdd;
        }
        return root;
    }

    public void swap(List<TreeNode> list) {
        int n = list.size();
        for(int i=0;i<n/2;++i) {
            int tmp = list.get(i).val;
            list.get(i).val = list.get(n-1-i).val;
            list.get(n-1-i).val = tmp;
        }
    }
}

[Leetcode] 2416. 字符串的前缀分数和

给你一个长度为 n 的数组 words ,该数组由 非空 字符串组成。
定义字符串 word 的 分数 等于以 word 作为 前缀 的 words[i] 的数目。
例如,如果 words = ["a", "ab", "abc", "cab"] ,那么 "ab" 的分数是 2 ,因为 "ab" 是 "ab" 和 "abc" 的一个前缀。
返回一个长度为 n 的数组 answer ,其中 answer[i] 是 words[i] 的每个非空前缀的分数 总和 。
注意:字符串视作它自身的一个前缀。

用字典树存储所有字符串,由于每个节点都是其子树节点的前缀,题干中的分数就是在字符串插入字典树的过程中,经过该节点的字符串个数,即以该节点为前缀的字符串的个数。

class Node {
    HashMap<Character, Node> children = new HashMap<>();;
    int score = 0;
}

class TrieTree {
    Node root = new Node();

    public void insert(String word) {
        Node cur = root;
        for(var ch:word.toCharArray()) {
            if(!cur.children.containsKey(ch)) cur.children.put(ch, new Node());
            cur = cur.children.get(ch);
            cur.score ++;
        }
    }

    public int searchScore(String word) {
        int res = 0;
        Node cur = root;
        for(var ch:word.toCharArray()) {
            if(!cur.children.containsKey(ch)) break;
            cur = cur.children.get(ch);
            res += cur.score;
        }
        return res;
    }
}

class Solution {
    public int[] sumPrefixScores(String[] words) {
        TrieTree tree = new TrieTree();
        for(var word:words) {
            tree.insert(word);
        }
        int n = words.length;
        int[] res = new int[n];
        for(int i=0;i<n;++i) {
            res[i] = tree.searchScore(words[i]);
        }
        return res;
    }
}

参考:
LeetCode

标签:TreeNode,cur,val,Contest,int,311,节点,字符串,Leetcode
From: https://www.cnblogs.com/hellojamest/p/16708897.html

相关文章

  • Weekly Contest 310
    WeeklyContest310ProblemAMostFrequentEvenElement思路水题,用map存一下每个偶数数量然后统计就行代码classSolution:defmostFrequentEven(self,nums:......
  • LeetCode — 跳跃游戏 III
    LeetCode—跳跃游戏III问题陈述给定一个非负整数数组arr,您最初位于开始数组的索引。当你在索引一世,你可以跳到i+arr[i]或者i—arr[i],检查是否可以......
  • LeetCode链表翻转
    SwapNodesinPairsLeetCode/力扣递归交换之后,直接交换下一个节点ListNode*swapPairs(ListNode*head){if(head&&head->next){swap(head->val,......
  • LeetCode深度优先搜索
    ValidateBinarySearchTreeLeetCode/力扣递归boolisValidBST(TreeNode*root){doublelower=DBL_MIN;doubleupper=DBL_MAX;returnhelper(root......
  • LeetCode广度优先搜索
    BinaryTreeLevelOrderTraversalLeetCode/力扣递归voidlevelOrderHelper(vector<vector<int>>&res,intlevel,TreeNode*root){if(root==NULL)return;......
  • Leetcode solution 2353. Design a Food Rating System
     ProblemStatement Designafoodratingsystemthatcandothefollowing:Modify theratingofafooditemlistedinthesystem.Returnthehighest-rated......
  • 2022 Jiangsu Collegiate Programming Contest
    A.PENTAKILL!把每个一个人的击杀序列分开,判断是否有连续五个不同的击杀就好#include<bits/stdc++.h>usingnamespacestd;map<string,vector<string>>st;int......
  • leetcode 2415.反转二叉树的奇数层
    leetcode2415.反转二叉树的奇数层题目描述给你一棵完美二叉树的根节点root,请你反转这棵树中每个奇数层的节点值。例如,假设第3层的节点值是[2,1,3,4,7,11,29,1......
  • LeetCode & SQL All In One
    LeetCode&SQLAllInOnehttps://leetcode.cn/study-plan/sql/?progress=sr9i61hrefs©xgqfrms2012-2020www.cnblogs.com/xgqfrms发布文章使用:只允许注册用......
  • Leetcode第8题:字符串转换整数 (atoi)
    /**这题就是要细心,首先要通过循环去掉前面的空格然后看看有没有正号或者负号,或者没有符号再看看数字有没有越界*/classSolution{publicintmyAtoi(Strings)......