39. Combination Sum
2016-02-28 08:31
295 查看
Backtracking问题
时间复杂度是指数级的,exponential
public List<List<Integer>> combinationSum(int[] candidates, int target) { List<List<Integer>> res = new ArrayList<List<Integer>>(); if(candidates == null || candidates.length == 0) { return res; } Arrays.sort(candidates); List<Integer> item = new ArrayList<Integer>(); helper(candidates, target, res, item, 0, 0); return res; } private void helper(int[] candidates, int target, List<List<Integer>> res, List<Integer> item, int start, int curSum) { if(start >= candidates.length || curSum > target) { return; } if(curSum == target) { if(!res.contains(item)) { res.add(new ArrayList<Integer>(item)); } return; } for(int i = start; i < candidates.length; i++){ item.add(candidates[i]); helper(candidates, target, res, item, i, curSum + candidates[i]); item.remove(item.size() - 1); } return; }
时间复杂度是指数级的,exponential
相关文章推荐
- 【extjs】 ext5 Ext.grid.Panel 分页,搜索
- postal.js使用
- 骆驼命名法,帕斯卡命名法和匈牙利命名法
- iOS开发--知识点总结
- Manthan, Codefest 16 633C Spy Syndrome 2(dfs + stl)
- Android 动画基础
- hdu 1201 18岁生日
- git
- 国内四个不错的技术团体博客
- 国内四个不错的技术团体博客
- XStream 学习笔记
- 蓝桥杯 BASIC 29 高精度加法(大数)
- OSChina 周日乱弹 ——冯小牛 我要治愈你!
- js正则表达式
- 国务院关于开展新型农村社会养老保险试点的指导意见
- shell命令执行顺序
- Cookie的细节具体保存的有效时间
- BZOJ 2467: [中山市选2010]生成树|组合数学
- AOP代理分析
- 【Android】15.0 第15章 广播和通知—本章示例主界面