lc-46


//Given an array nums of distinct integers, return all the possible permutations
//. You can return the answer in any order. 
//
// 
// Example 1: 
// Input: nums = [1,2,3]
//Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
// Example 2: 
// Input: nums = [0,1]
//Output: [[0,1],[1,0]]
// Example 3: 
// Input: nums = [1]
//Output: [[1]]
// 
// 
// Constraints: 
//
// 
// 1 <= nums.length <= 6 
// -10 <= nums[i] <= 10 
// All the integers of nums are unique. 
// 
// Related Topics Array Backtracking 
// 👍 11542 👎 205


//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> results = new ArrayList<>();
        List<Integer> curResult = new ArrayList<>();
        getNext(results, curResult, new boolean[nums.length], nums);
        return results;
    }

    private void getNext(List<List<Integer>> results, List<Integer> curResult, boolean[] visited, int[] nums) {
        if (curResult.size() == nums.length) {
            List<Integer> oneR = new ArrayList<>(curResult);
            results.add(oneR);
        }

        for (int i = 0; i < nums.length; i++) {
            if (!visited[i]) {
                curResult.add(nums[i]);
                visited[i] = true;
                getNext(results, curResult, visited, nums);
                curResult.remove(curResult.size() - 1);
                visited[i] = false;
            }
        }
    }
}
//leetcode submit region end(Prohibit modification and deletion)

文章作者: 倪春恩
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 倪春恩 !