回溯问题,注意从回溯到上一层时,要恢复列表的状态
ArrayList<ArrayList<Integer>> resList = new ArrayList<>(); public ArrayList<ArrayList<Integer>> permute(int[] num) { if (num == null){ return null; } ArrayList<Integer> curlist = new ArrayList<>(); findPerm(num,curlist); return resList; } private void findPerm(int[] num, List<Integer> list){ if (list.size() == num.length){ resList.add(new ArrayList<>(list)); return; } for (int i = 0; i < num.length; i++) { if (!list.contains(num[i])){ list.add(num[i]); findPerm(num,list); list.remove(list.size() - 1); } } }