[JAVA] use recursion stupidly....


  • 0
    H
    public class Solution {
        ArrayList<ArrayList<Integer>> l=new ArrayList<>();
        
        public List<List<Integer>> permute(int[] nums) {
            ArrayList<Integer> b=new ArrayList<>();
            for(int i=0;i<nums.length;i++){
                b.add(nums[i]);
            }
            ArrayList<Integer> c=new ArrayList<>();
            into(b,c);
            return (List)l;
        }
        
        public void into(ArrayList b, ArrayList c){
            if(b.size()==0){
                l.add(c);
                return;
            }
            for(int i=0;i<b.size();i++){
                ArrayList cc=(ArrayList)(c.clone());
                ArrayList bb=(ArrayList)(b.clone());
                cc.add(b.get(i));
                bb.remove(i);
                into(bb,cc);
            }
        }
    }

Log in to reply
 

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