650. Keyboard with only two keys

The sum of prime factors

var minSteps = function(n) {
    if (n == 1) return 0
    let count = 0, res = n
    for (let i = 2; i <= res / 2;) {// The initial value of n
        if (n % i == 0) {
            count += i
            n /= i
        }
        else i++
    }
    return count == 0 ? res : count
};
Copy the code

46. The whole arrangement

var permute = function(nums) {
    const res = []
    const used = {}
    function dfs(path) {
        if (path.length == nums.length) {
            res.push(path.slice())
            return
        }
        for (const num of nums) {
            if (used[num]) continue
            path.push(num)
            used[num] = true
            dfs(path)
            path.pop()
            used[num] = false
        }
    }

    dfs([])
    return res
};
Copy the code