My easy understanding solution


  • 0
    B
    public List<List<Integer>> permute(int[] nums) {
            List<List<Integer>> res = new ArrayList<>();
    		int len =nums.length;
    		int j,len2;
    		if (len==0) {
    			return res;
    		}
    		List<List<Integer>> tmpRes = new ArrayList<>();
    		List<Integer> tmpList = null;
    		res.add(new ArrayList<>());
    		res.get(0).add(nums[0]);
    		for(int i=1;i<len;i++){
    			tmpRes.clear();
    			for(List<Integer> tmp:res){
    				len2 = tmp.size();
    				for(j=0;j<=len2;j++){
    					tmpList = new ArrayList<>();
    					tmpList.addAll(tmp);
    					tmpList.add(j, nums[i]);
    					tmpRes.add(tmpList);
    				}
    			}
    			res.clear();
    			res.addAll(tmpRes);
    		}
    		return res;
        }

Log in to reply
 

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