Zheaoli / do-something-right

MIT License
37 stars 3 forks source link

2022-08-10 #325

Open Zheaoli opened 1 year ago

Zheaoli commented 1 year ago

2022-08-10

gongpeione commented 1 year ago
/*
 * @lc app=leetcode id=617 lang=typescript
 *
 * [617] Merge Two Binary Trees
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     val: number
 *     left: TreeNode | null
 *     right: TreeNode | null
 *     constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.left = (left===undefined ? null : left)
 *         this.right = (right===undefined ? null : right)
 *     }
 * }
 */

function mergeTrees(root1: TreeNode | null, root2: TreeNode | null): TreeNode | null {
    if (!root1 && !root2) {
        return null;
    }
    const newRoot = new TreeNode(
        (root1?.val || 0) + (root2?.val || 0),
        mergeTrees(root1?.left, root2?.left),
        mergeTrees(root1?.right, root2?.right)
    );

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

微信id: 弘树 来自 vscode 插件

restartgr commented 1 year ago
/*
 * @lc app=leetcode.cn id=236 lang=javascript
 *
 * [236] 二叉树的最近公共祖先
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @param {TreeNode} p
 * @param {TreeNode} q
 * @return {TreeNode}
 */
var lowestCommonAncestor = function (root, p, q) {
    const travel = (root, p, q) => {
        if (!root || root === q || root === p) {
            return root
        }
        let left = travel(root.left, p, q)
        let right = travel(root.right, p, q)
        if (left && right) {
            return root
        }
        if (!left) {
            return right
        }
        return left
    }
    return travel(root, p, q)
};
// @lc code=end

微信id: Jörmungandr 来自 vscode 插件

SaraadKun commented 1 year ago

640. 求解方程

class Solution {
    public String solveEquation(String equation) {
        //equation 只有一个 '='.
        String[] eqs = equation.split("=");
        //分别统计等式两边的表达式,处理为ax+b的形式,以数组形式返回
        int[] eq1 = process(eqs[0]);
        int[] eq2 = process(eqs[1]);
        int coe = eq1[0] - eq2[0], con = eq2[1] - eq1[1];
        if (coe == 0) {
            return con == 0 ? "Infinite solutions" : "No solution";
        }
        return "x=" + con/coe;
    }

    // int[] arr, arr[0]为x系数,arr[1]为常数项
    private int[] process(String eq) {
        char[] chs = eq.toCharArray();
        int[] ans = new int[2];
        int n = chs.length, sign = 1, idx = 1, cur = 0;
        for (int i = 0; i < n; i++) {
            char ch = chs[i];
            if ('-' == ch || '+' == ch) {
                ans[idx] += cur * sign;
                cur = 0;
                idx = 1;
                sign = ch == '-' ? -1 : 1;
                continue;
            }
            if (ch == 'x') {
                cur = cur == 0 ? (i > 0 && chs[i - 1] == '0' ? 0 : 1) : cur;
                idx = 0;
            } else {
                cur = cur * 10 + ch - '0';
            }
        }
        ans[idx] += cur * sign;
        return ans;
    }
}

WeChat:Saraad