首页 > 其他 > 详细

LeetCode Permutations II

时间:2015-09-27 13:38:09      阅读:132      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!