Welcome to my blog

arrangement

From n distinct elements, any m distinct elements are arranged in a row in a certain order, which is called a permutation of m elements taken from n distinct elements. The number of permutations of m(m≤n) elements from n different elements is called the number of permutations of m elements from n different elements, using notationA(n, m)Represents, additionally stipulates0! = 1

combination

From n different elements, any m(m≤n) elements are taken to form a group, which is called a combination of m elements taken from n different elements; The number of all combinations of m(m≤n) elements from n distinct elements is called the number of combinations of m elements from n distinct elements. Use symbolsC(n,m)saidC(n,m)=C(n,n-m)

Common Topics

A subset of

Title source

Depth0: [1], [] depth1: [2], [] depth2: [3], [] depth3: back
Const subsets = nums => {const result = [] // Path = [] const DFS = (depth) => {// If (depth === nums.length) {result.push(path.slice()) return} // take the current node value and go down Pass.push (nums[depth]) DFS (depth + 1) // do not take the current value and go down the path. Pop () DFS (depth + 1)} DFS (0) return result}

The whole arrangement

Title source

Thinking: [1, 2, 3] - > [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]] depth: choice: depth0: [[1], [2], [3]] intersection used[] depth1: [[1], [2], [3]] intersection used[choice1] depth2: [[1], [2], [3]] intersecting used[choice1, choice2] depth3
const permute = nums => { const used = new Set() const path = [] const result = [] const dfs = (depth) => { if (depth === nums.length) { result.push(path.slice()) return } for (let i = 0; i < nums.length; i++) { if (! used.has(i)) { used.add(i) path.push(nums[i]) dfs(depth + 1) used.delete(i) path.pop() } } } dfs(0) return result }

Parentheses generates

Title source

Thinking: n = 3 [" ((())) ", "() () ()", "() () ()", "() () ()", "() () ()"] depth0: = > < n left && (left) or right && (right (left) depth1: Depth2: => left && (left < n) or right < left) depth2: => left && (left < n) or right < left).. depth2n
const generateParenthesis = function (n) {
    const result = []
    const path = []
    let left = 0, right = 0
    const dfs = depth => {
        if (depth === 2 * n) {
            result.push(path.slice().join(''))
            return
        }
        if (left < n) {
            path.push('(')
            left++
            dfs(depth + 1)
            path.pop()
            left--
        }
        if (right < left) {
            right++
            path.push(')')
            dfs(depth + 1)
            path.pop()
            right--
        }
    }
    dfs(0)
    return result
}

Combined total

Title source

Ideas: < / span > < / span > < / span > < / span > < / span > < / span > < / span > < / span > Result = [[0, 0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0] back
const combinationSum = (candidates, target) => { const result = [] const path = [] const dfs = (depth) => { const cp = path.slice() const sum = cp.reduce((acc, item) => acc + item, 0) if (sum === target) { result.push(cp) return } if (sum > target) { return } for (let i = 0; i < candidates.length; i++) { path.push(candidates[i]) dfs(depth + 1) path.pop() } } dfs(0) const set = new Set() return result.filter(item => { let val = item.sort().join("") if (! set.has(val)) { set.add(val) return true } return false }) }

Restore IP address

(https://leetcode-cn.com/probl…

Image rendering

(https://leetcode-cn.com/probl…

Word search

(https://leetcode-cn.com/probl…

Restore IP address

(https://leetcode-cn.com/probl…

N queen

(https://leetcode-cn.com/probl…

The number of islands

(https://leetcode-cn.com/probl…