My JAVA solution


  • -3
    Q
    public class Solution {
        public List<List<Integer>> permuteUnique(int[] nums) {
    		List<Integer> numList = new ArrayList<Integer>();
    		for (int i=0;i<nums.length;i++){
    			numList.add(nums[i]);
    		}
    		List<List<Integer>> results = new ArrayList<List<Integer>>();
    		calculate(numList, null, results);
    		return results;
    	}
    	
    	private void calculate(List<Integer> numList, Node parent, List<List<Integer>> results){
    		if (!numList.isEmpty()){
        		Set<Integer> used = new HashSet<Integer>();
        		for (int i=0;i<numList.size();i++){
        			Integer val = numList.get(i);
        			if (used.contains(val)){
        				continue;
        			}
        			used.add(val);
        			numList.remove(i);
        			Node n = new Node(val, parent);
        			calculate(numList, n, results);
        			numList.add(i,val); //trackback
        		}
    		}
    		else{
    		    results.add(parent.getPermutation());
    		}
    	}
    	
    	private class Node{
    		int val;
    		Node parent;
    		public Node(int val, Node parent){
    			this.parent = parent;
    			this.val = val;
    		}
    		public List<Integer> getPermutation(){
    			List<Integer> list = new ArrayList<Integer>();
    			list.add(val);
    			Node n = parent;
    			while (n!=null){
    				list.add(n.val);
    				n = n.parent;
    			}
    			return list;
    		}
    	}
    }

Log in to reply
 

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