java recursion solution


  • 0
    P
    public class Solution {
        List<List<Integer>> list = new ArrayList<>();
        boolean[] bool;
        int[] nums;
        int count = 0;
        public List<List<Integer>> permute(int[] nums) {
            this.nums = nums;
            bool = new boolean[nums.length];
            recursion(new ArrayList<Integer>());
            return list;
            
        }
        private void recursion(List<Integer> sublist)
        {
            if(count == nums.length)
            {
                list.add(new ArrayList<Integer>(sublist));
                return;
            }
            for(int i = 0;i<nums.length;i++)
            {
                if(!bool[i])
                {
                    bool[i] = true;
                    count++;
                    sublist.add(nums[i]);
                    recursion(sublist);
                    sublist.remove(sublist.size()-1);
                    count--;
                    bool[i] = false;
                }
            }
            return;
        }
    }
    

Log in to reply
 

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