220ms Java iteration solution, easy to understand


  • 0
    S
    public class Solution {
        public List<List<Integer>> permute(int[] num) {
            List<List<Integer>> wrapList = new LinkedList<List<Integer>>();
            List<List<Integer>> preList = null;
            List<Integer> subList = new LinkedList<Integer>();
            if(num.length == 0) return wrapList;
            subList.add(num[0]);
            wrapList.add(subList);
            preList = wrapList;
            for(int i=1; i<num.length; i++) {
                wrapList = new LinkedList<List<Integer>>();
                for(int j=0; j<preList.size(); j++) {
                    for(int k=0; k<=preList.get(j).size(); k++) {
                        subList = new LinkedList<Integer>(preList.get(j));
                        subList.add(k, num[i]);
                        wrapList.add(subList);
                    }
                }
                preList = wrapList;
            }
            return wrapList;
        }
    }

Log in to reply
 

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