回溯算法

105 阅读1分钟
  let nums = [1, 2, 5]
        function huisuo(nums) {
            let len = nums.length;
            let res = [];
            function backTrace(path) {
                if (path.length == len) return res.push(path.slice());
                for (let i = 0; i < len; i++) {
                    if (path.indexOf(nums[i]) === -1) {
                        path.push(nums[i]);
                        backTrace(path);
                        path.pop()
                    }
                }

            }
            backTrace([])
            return res;
        }
        console.log(huisuo(nums));