码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode Permutations II

时间:2015-09-27 13:39:45      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:

原题链接在这里:https://leetcode.com/problems/permutations-ii/

Permutations的进阶题目。Iteration方法和Subsets II很像,加进newRes之前检查newRes是否包含了重复item, 没有重复才可以加进来。

AC Java:

 1 public class Solution {
 2     public List<List<Integer>> permuteUnique(int[] nums) {
 3         List<List<Integer>> res = new ArrayList<List<Integer>>();
 4         if(nums == null || nums.length == 0){
 5             return res;
 6         }
 7         List<Integer> item = new ArrayList<Integer>();
 8         item.add(nums[0]);
 9         res.add(item);
10         for(int i = 1; i<nums.length; i++){
11             List<List<Integer>> newRes = new ArrayList<List<Integer>>();
12             for(int j = 0; j<res.size(); j++){
13                 List<Integer> cur = res.get(j);
14                 for(int k=0;k<cur.size()+1;k++){
15                     item = new ArrayList<Integer>(cur);
16                     item.add(k,nums[i]);
17                     if(!newRes.contains(item)){
18                         newRes.add(item);
19                     }
20                 }
21             }
22             res = newRes;
23         }
24         return res;
25     }
26 }

 

LeetCode Permutations II

标签:

原文地址:http://www.cnblogs.com/Dylan-Java-NYC/p/4842119.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!