Subject description:
Given a set of integer array elements no repeating the nums , which returns an array of all possible subsets (power set).
Description: Solution Set can not contain duplicate subsets.
Example: Input: nums = [1,2,3] Output: [ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ]
Topic Analysis: backtracking process is to perform a depth-first traversal, a way to go in the end, dead end when, returned back, continue, continue like this until you return to the starting point.
Code:
class Solution { public static List<List<Integer>> subsets(int[] nums) { List<List<Integer>> res = new ArrayList<>(); backtrack(0, nums, res, new ArrayList<>()); return res; } private static void backtrack(int i, int[] nums, List<List<Integer>> res, ArrayList<Integer> tmp) { res.add(new ArrayList<>(tmp)); for (int j = i; j < nums.length; j++) { tmp.add(nums[j]); backtrack(j + 1, nums, res, tmp); tmp.remove(tmp.size() - 1); } } }