Share my easy understanding but dirty java accepted code.


  • 0
    O

    Share my easy understanding but dirty java accepted code.

    Anyone have some suggestions to improve this?

    public class Solution {
        public List<List<Integer>> permute(int[] nums) {
            
            List<List<Integer>> res = new ArrayList<List<Integer>>();
            
            
            List<Integer> c = new ArrayList<Integer>();
            
            
            dfs(res, nums, c);
            
            return res;
            
        }
        
        
        public void dfs(List<List<Integer>> res, int[] nums, List<Integer> curr){
            
            if (curr.size() == nums.length){
                if (!res.contains(curr)){
                    
                    res.add(curr);
                }
                return;
            }
            
            for (int j = 0; j < nums.length; j++){
                
                List<Integer> n = new ArrayList<Integer>(curr);
    
                if ( !n.contains(nums[j]) ){
                    n.add(nums[j]);
                    dfs(res, nums, n);
                }
    
            }
        }
    }

Log in to reply
 

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