Java Backtracking Simple Solution


  • 0
    A
    public class Solution {
        
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        Set<List<Integer>> set = new HashSet<List<Integer>>();
        public List<List<Integer>> findSubsequences(int[] nums) {
            List<Integer> list = new ArrayList<Integer>();
            for(int i=0;i<nums.length;i++){
                list.add(nums[i]);
                helper(nums, i+1, list);
                list.clear();
            }
            result.addAll(set);
            return result;
        }
        
        public void helper(int[] nums, int index, List<Integer> list){
            if(list.size() >= 2){
                set.add(new ArrayList<Integer>(list));
            }
            for(int i=index;i<nums.length;i++){
                if(nums[i] >= list.get(list.size()-1)){
                    list.add(nums[i]);   
                    helper(nums, i+1, list);
                    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.