47. Permutations II

只比1多了一句话

if(i > 0 && used[i-1] == true && nums[i] == nums[i-1]){
                 continue;
}

 

 

 1 public List<List<Integer>> permute(int[] nums) {
 2         List<List<Integer>> res = new ArrayList<List<Integer>>();
 3         if(nums == null || nums.length == 0) {
 4             return res;
 5         }
 6         Arrays.sort(nums);
 7         boolean[] used = new boolean[nums.length];
 8         helper(nums, res, new ArrayList<Integer>(), used);
 9         return res;
10     }
11     
12     private void helper(int[] nums, List<List<Integer>> res, List<Integer> item, boolean[] used) {
13         if(item.size() == nums.length) {
14             if(!res.contains(item)) {
15                 res.add(new ArrayList<Integer>(item));
16             }
17             return;
18         }
19         for(int i = 0; i < nums.length; i++) {
20             if(i > 0 && used[i-1] == true && nums[i] == nums[i-1]){
21                 continue;
22             }
23             if(used[i] != true) {
24                 used[i] = true;
25                 item.add(nums[i]);
26                 helper(nums, res, item, used);
27                 item.remove(item.size() - 1);
28                 used[i] = false;
29             }
30         }
31         return;
32     }

 

posted @ 2016-02-29 04:15  warmland  阅读(107)  评论(0编辑  收藏  举报