登录
  • #刷题
  • #leetcode

permutationII疑问

zh355245849
1561
10
public class Solution {[br][/br][br][/br]    public List<List<Integer>> permuteUnique(int[] nums) {[br][br][/br]        List<List<Integer>> res = new ArrayList<List<Integer>>();[br][/br][br][/br]        List<Integer> rows = new ArrayList<Integer>();[br][/br][br][/br]        if(nums == null || nums.length == 0)[br][/br][br][/br]            return res;[br][/br][br][/br]        Arrays.sort(nums);[br][/br][br][/br]        boolean[] record = new boolean[nums.length];[br][/br][br][/br]        dfs(nums, record, res, rows);[br][/br][br][/br]        return res;[br][/br][br][/br]    }[br][/br][br][/br]    public void dfs(int[] nums, boolean[] record, List<List<Integer>> res, List<Integer> rows) {[br][/br][br][/br]        if(nums.length == rows.size()) {[br][/br][br][/br]            if(!res.contains(rows))[br][/br][br][/br]            res.add(new ArrayList<Integer>(rows));[br][/br][br][/br]            return ;[br][/br][br][/br]        }[br][/br][br][/br]        for(int i = 0; i < nums.length; i++) {[br][/br][br][/br]            //if(i > 0 && nums[i]==nums[i-1] && record[i] == false)[br][/br][br][/br]              //  continue;            [br][/br][br][/br]            if(record[i] == false) {[br][/br][br][/br]                rows.add(nums[i]);[br][/br][br][/br]                record[i] = true;[br][/br][br][/br]                dfs(nums, record, res, rows);[br][/br][br][/br]                record[i] = false;[br][/br][br][/br]                rows.remove(rows.size() - 1);[br][/br][br][/br]                //while(i < nums.length - 1 && nums[i] == nums[i+1])[br][/br][br][/br]                  //  i++;[br][/br][br][/br]            }[br][/br][br][/br]            [br][/br][br][/br]        }[br][/br][br][/br]        return ;[br][/br][br][/br]    }[br][/br][br][/br]}各路大神,我想问下这道题这样写为什么会超时。。。写subsetII时用contains明明是可以通过的,这里为什么不行了。。。谢谢了![/i][/i][/i][/i][/i][/i][/i]
10条回复
热度排序

发表回复