Simple Java Solution


  • 0
    K
    public class Solution {
        
        public List<List<Integer>> findSubsequences(int[] nums) {
                    List<List<Integer>> ls = new ArrayList<List<Integer>>();
    		Set<List<Integer>> s = new HashSet<List<Integer>>();
    		findSubsequences1(nums, 0, new ArrayList<Integer>(), s);
    		ls.addAll(s);
    		return ls;
    	}
    	 
    	 private void findSubsequences1(int[] arr, int j, List<Integer> list, Set<List<Integer>> s) {
    		 for(int i=j;i<arr.length;i++) {
    			list.add(arr[i]);
    			if(list.size() > 1) {
    				int k = 0;
    				while(k<list.size()-1 && list.get(k)<=list.get(k+1)) {
    					k++;
    				}
    				if(k==list.size()-1) s.add(new ArrayList<Integer>(list));
    			}
    			findSubsequences1(arr, i+1, list, s);
    			list.remove(list.size()-1);
    		}
    	}
    }
    

Log in to reply
 

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