琼海网站制作怎么做公司网站
78. 子集 - 力扣(LeetCode)
使用dfs,用一个变量记录当前开始遍历的位置,下一次递归从当前位置+1开始递归,然后加入集合中,可以保证不会重复,回溯时直接移除最后的元素即可。
最开始的代码是没有记录位置的变量,每一次循环递归判断当前集合是否在目的集合中,而且需要排序,导致时间复杂度过高,而且在dfs中进行排序后,在移除最后的数据,数据会发生变化,导致移除了不该移除的数据导致结果错误。如果不进行排序,而在主函数中在遍历所有的结果进行排序移除重复集合,时间复杂度会过高导致超时。
超时的代码:
class Solution {List<List<Integer>> list;List<Integer> res;public List<List<Integer>> subsets(int[] nums) {res = new LinkedList<>();list = new LinkedList<>();list.add(new LinkedList<>());for (int i = 0; i < nums.length; i++) {res = new LinkedList<>();res.add(nums[i]);dfs(res,nums);}List<List<Integer>> result = new LinkedList<>();for (int i = 0; i < list.size(); i++) {List<Integer> list1 = list.get(i);Collections.sort(list1);if (!result.contains(list1)) {result.add(list1);}}return result;}public void dfs(List<Integer> res,int[] nums) {if (!list.contains(res) && res.size() <= nums.length) {list.add(new LinkedList<>(res));System.out.println(res);}for (int i = 0; i < nums.length; i++) {if (!res.contains(nums[i])) {res.add(nums[i]);dfs(res,nums);res.remove(res.size() - 1);}}}
}
优化后的代码:
class Solution {static List<List<Integer>> list;static List<Integer> res;public List<List<Integer>> subsets(int[] nums) {res = new LinkedList<>();list = new LinkedList<>();// int[] nums = new int[]{1, 2, 3};int index = 0;dfs(res, nums, index);return list;}
public static void dfs(List<Integer> res, int[] nums, int index) {list.add(new LinkedList<>(res));for (int i = index; i < nums.length; i++) {res.add(nums[i]);dfs(res, nums, i + 1);res.remove(res.size()-1);}}
}