My Iterative AC Solution


  • 1
    G
    public List<List<Integer>> permute(int[] num) {
        List<List<Integer>> result = new ArrayList();
        List<List<Integer>> newList;
        
        if(num == null || num.length < 0)
            return result;
        List<Integer> tmp = new ArrayList();
        tmp.add(num[0]);
        result.add(tmp);
        
        for(int i = 1; i < num.length; i++){
            newList = new ArrayList();
            while(!result.isEmpty()){
                tmp = result.remove(0);
                for(int j = 0; j <= tmp.size(); j++){
                    tmp.add(j, num[i]);
                    newList.add(new ArrayList(tmp));
                    tmp.remove(j);
                }
            }
            result = newList;
        }
        
        return result;
    }

Log in to reply
 

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