Java 3ms short solution


  • 0
    D
    public class Solution {
    	public List<List<Integer>> permute( int[] nums ) {
           List<List<Integer>> ret = new ArrayList<List<Integer>>();	
           next_permute( ret, nums, 0 );
           return ret;
    }
    
    public void next_permute(List<List<Integer>> ret,int[] nums,int p){
    	if(p==nums.length-1){
    		List<Integer> list = new ArrayList<Integer>();
    		for(int i=0;i<nums.length;i++){
    			list.add( nums[i] );
    		}
    		ret.add( list );
    	}
    	else{
    		for(int i=p;i<nums.length;i++){
    			swap(nums,i,p);
    			next_permute( ret, nums, p+1 );
    			swap(nums,i,p);
    		}
    	}
    }
    
    private void swap(int[] nums,int i,int j){
    	int t = nums[i];
    	nums[i] = nums[j];
    	nums[j] = t;
    }
    

    }


Log in to reply
 

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