首页 > 其他 > 详细

491 Increasing Subsequences

时间:2018-08-10 15:34:23      阅读:201      评论:0      收藏:0      [点我收藏+]
491 Increasing Subsequences



class Solution {
    public List<List<Integer>> findSubsequences(int[] nums) {
      Set<List<Integer>> set = new HashSet<>();
      
      List<Integer> tempList = new ArrayList<>();
      
      
      dfs(set, tempList, nums, 0);
      return new ArrayList<>(set); // new ArrayList<>();
        
    }
  
    private void dfs(Set<List<Integer>> set, List<Integer> tempList, int[] nums, int index){
      if(tempList.size() > 1){
        set.add(new ArrayList<>(tempList));
      }
      
      // add numbers
      for(int i = index; i < nums.length; i++){
        if(tempList.size() == 0 || tempList.get(tempList.size() - 1) <= nums[i]){
          tempList.add(nums[i]);
          dfs(set, tempList, nums, i + 1 ); // be careful , not index + 1 
          tempList.remove(tempList.size() - 1);
        }
      }
    }
}


https://www.youtube.com/watch?v=dl68syidyLM&t=242s 


注意reference 写法。。这个还不熟
画 dfs 图


https://leetcode.com/problems/increasing-subsequences/discuss/97130/Java-20-lines-backtracking-solution-using-set-beats-100.

 

491 Increasing Subsequences

原文:https://www.cnblogs.com/tobeabetterpig/p/9454918.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!