Short Java Iterative Solution (15-line)


  • 1
    public List<List<Integer>> findSubsequences(int[] nums) {
        Set<List<Integer>> results = new HashSet<List<Integer>>();
        Set<List<Integer>> subResults = new HashSet<List<Integer>>();
        for (int num : nums) {
            for (List<Integer> l : subResults) {
                if (l.get(l.size() - 1) > num) continue;
                List<Integer> copy = new ArrayList<Integer>(l);
                copy.add(num);
                results.add(copy);
            }
            subResults.add(Arrays.asList(num));
            subResults.addAll(results);
        }
        return new LinkedList(results);
    }
    

Log in to reply
 

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