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


  • 0
    L

    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>();
        	start.add(nums[0]);
        	out.add(start);
        	for (int pos = 1; pos < nums.length; pos++){
    	    	ArrayList<ArrayList<Integer>> tmp = new ArrayList<ArrayList<Integer>>(out);
    	    	out.clear();
    	    	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]);
    	    			out.add(chk);
    	    		}
    	    	}
        	}
        	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.