Zheaoli / do-something-right

MIT License
37 stars 3 forks source link

2022-09-02 #348

Open Zheaoli opened 2 years ago

Zheaoli commented 2 years ago

2022-09-02

SaraadKun commented 2 years ago

687. 最长同值路径

class Solution {

    int ans = 0;
    public int longestUnivaluePath(TreeNode root) {
        dfs(root, -1001);
        return ans;
    }

    private int dfs(TreeNode root, int val) {
        if (root == null) {
            return 0;
        }
        int left = dfs(root.left, root.val), right = dfs(root.right, root.val);
        ans = Math.max(left + right, ans);
        if (root.val == val) {
            return Math.max(left, right) + 1;
        }
        return 0;
    }
}

WeChat: Saraad

Dapeus commented 2 years ago

image

gongpeione commented 2 years ago
/*
 * @lc app=leetcode id=191 lang=typescript
 *
 * [191] Number of 1 Bits
 */

// @lc code=start
function hammingWeight(n: number): number {
    let count = 0;

    while (n) {
        if (n & 1) {
            count++;
        }
        n = n >>> 1;
    }

    return count;
};
// @lc code=end

微信id: 弘树 来自 vscode 插件