77. Combinations

是我熟悉的backtracking,但是依旧犯了蠢错误,调了半天

不过尝试了一种:如果不能有重复数字数字的时候,就在主函数里面设一个boolean数字,名字是used,意义如名字显示,当做一个参数传给helper函数

 1     public List<List<Integer>> combine(int n, int k) {
 2     List<List<Integer>> res = new ArrayList<List<Integer>>();
 3         if(k > n) {
 4             return res;
 5         }
 6         helper(res, new ArrayList<Integer>(), n, k, 1);
 7         return res;
 8     }
 9     
10     private void helper(List<List<Integer>> res, List<Integer> item, int n, int k, int start) {
11         if(item.size() == k) {
12             res.add(new ArrayList(item));
13             return;
14         }
15         for(int i = start; i <= n; i++) {
16             item.add(i);
17             helper(res, item, n, k, i + 1);
18             item.remove(Integer.valueOf(i));
19         }
20     }

蠢错误在17行,helper(res, item, n, k, i+1)最后一个参数被我typo成start…………= =心累

posted @ 2016-03-31 06:32  warmland  阅读(157)  评论(0编辑  收藏  举报