Zheaoli / do-something-right

MIT License
37 stars 3 forks source link

2022-08-06 #321

Open Zheaoli opened 2 years ago

Zheaoli commented 2 years ago

2022-08-06

gongpeione commented 2 years ago
/*
 * @lc app=leetcode id=101 lang=typescript
 *
 * [101] Symmetric Tree
 */

// @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)
 *     }
 * }
 */

// abstract class TreeNode {
//     val: number
//     left: TreeNode | null
//     right: TreeNode | null
// }

function isSymmetric(root: TreeNode | null): boolean {
    let [l = null, r = null] = [root?.left, root?.right];

    const walk = (l: TreeNode | null, r: TreeNode | null) => {
        if (!l && !r) return true;
        if (!l || !r) return false;
        return l.val === r.val
            && walk(l.left, r.right)
            && walk(l.right, r.left);
    }

    return walk(l, r);
};

// @lc code=end

微信id: 弘树 来自 vscode 插件

SaraadKun commented 2 years ago

1408. 数组中的字符串匹配

class Solution {
    public List<String> stringMatching(String[] words) {
        List<String> ans = new ArrayList<>();
        Arrays.sort(words, (o1, o2) -> o2.length() - o1.length());
        for (int i = 1; i < words.length; i++) {
            for (int j = 0; j < i; j++) {
                if (words[j].contains(words[i])) {
                    ans.add(words[i]);
                    break;
                }
            }
        }
        return ans;
    }
}

WeChat: Saraad