Sorting Without Using HashSet (Java Solution)


  • 0
    S

    when consecutive numbers appear, gradually grab one into the group of numbers being added to previous level.

    public class Solution {
        public List<List<Integer>> subsets(int[] S) {
            List<List<Integer>> list = new ArrayList<List<Integer>>();
            List<Integer> zero = new ArrayList<Integer>();
            list.add(zero);
            Arrays.sort(S);
            for(int i=0,j=1;i < S.length;i+=j){
                int k = list.size();
                j = 1;
                while(i+j < S.length && S[i+j]==S[i]) j++; // j = # of consecutive numbers
                for(int t = 1;t <= j;t++){ // consecutive numbers
                    for(int u = 0;u < k;u++){ // loop previous list
                        List<Integer> sub = new ArrayList<Integer>(list.get(u));
                        for(int num = 0;num < t;num++) sub.add(S[i]);
                        list.add(sub);
                    }
                }
            }
            return list;
        }
    }

Log in to reply
 

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