Zheaoli / do-something-right

MIT License
37 stars 3 forks source link

2022-09-22 #368

Open Zheaoli opened 2 years ago

Zheaoli commented 2 years ago

2022-09-22

Dapeus commented 2 years ago

image

dreamhunter2333 commented 2 years ago
#
# @lc app=leetcode.cn id=1640 lang=python3
#
# [1640] 能否连接形成数组
#
from typing import List

# @lc code=start
class Solution:
    def canFormArray(self, arr: List[int], pieces: List[List[int]]) -> bool:
        arr_map = {num: i for i, num in enumerate(arr)}
        for piece in pieces:
            pre_index = None
            for num in piece:
                if num not in arr_map or (
                    pre_index is not None and
                    arr_map[num] - pre_index != 1
                ):
                    return False
                pre_index = arr_map[num]
        return True

# @lc code=end
print(Solution().canFormArray(arr=[15, 88], pieces=[[88], [15]]))
print(Solution().canFormArray(arr=[49, 18, 16], pieces=[[16, 18, 49]]))
print(Solution().canFormArray(
    arr=[91, 4, 64, 78], pieces=[[78], [4, 64], [91]]
))

微信id: 而我撑伞 来自 vscode 插件

gongpeione commented 2 years ago
/*
 * @lc app=leetcode id=22 lang=typescript
 *
 * [22] Generate Parentheses
 */

// @lc code=start
function generateParenthesis(n: number): string[] {
    // store parenthesis group
    let stack = [] as string[];
    // all result
    const ans = [] as string[];

    const gen = (leftCount = 0, rightCount = 0) => {
        if (leftCount === rightCount && leftCount === n) {
            ans.push(stack.join(''));
            return;
        }

        if (leftCount < n) {
            stack.push('(');
            gen(leftCount + 1, rightCount);
            stack.pop();
        }

        if (leftCount > rightCount) {
            stack.push(')');
            gen(leftCount, rightCount + 1);
            stack.pop();
        }
    }

    gen();

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

微信id: 弘树 来自 vscode 插件