Can I just use Set to solve this problem (only need a very small modification on permutation I)?

  • 0

    If Set is allowed, this problem will be pretty easy (code like below, accepted and not too slow), but this seems make the problem not be a hard level.

    public class Solution {
        public List<List<Integer>> permuteUnique(int[] nums) {
            Set<ArrayList<Integer>> out = new HashSet<ArrayList<Integer>>();
        	List<List<Integer>> result = new ArrayList<List<Integer>>();
        	if (nums == null || nums.length == 0){
        		return result;
        	ArrayList<Integer> start = new ArrayList<Integer>();
        	for (int pos = 1; pos < nums.length; pos++){
    	    	ArrayList<ArrayList<Integer>> tmp = new ArrayList<ArrayList<Integer>>(out);
    	    	for (ArrayList<Integer> t : tmp){
    	    		for (int j = 0; j <= t.size(); j++){
    	    			ArrayList<Integer> chk = new ArrayList<Integer>(t);
    	    			chk.add(j, nums[pos]);
        	result = new ArrayList<List<Integer>>(out);
        	return result;

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.