leetcode Combination Sum II
2015-10-23 19:08
399 查看
题目链接
思路:
其实就是在递归的时候避免开始递归的点和现在的点是相同的就好。也就是代码
这里实现的东西。。
思路:
其实就是在递归的时候避免开始递归的点和现在的点是相同的就好。也就是代码
int k=i++; while(i<n&&candidates[i]==candidates[k]) { i++; }
这里实现的东西。。
public class Solution {
int [] candidates;
int n;
LinkedList<List<Integer>> result;
LinkedList<Integer>temp;
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
Arrays.sort(candidates);
this.candidates=candidates;
n=candidates.length;
result=new LinkedList<List<Integer>>();
temp=new LinkedList<Integer>();
help(0,target);
return result;
}
void help(int start,int left)
{
int i=start;
while(i<n)
{
int currentLeft=left-candidates[i];
if(currentLeft<0)
{
return;
}
temp.add(candidates[i]);
if(currentLeft>0)
{
int k=i+1;
if(k<n)
{
help(k,currentLeft);
}
}
else// if(currentLeft==0)
{
result.add(new LinkedList<Integer>(temp));
}
temp.removeLast();
int k=i++;public class Solution {
int [] candidates;
int n;
LinkedList<List<Integer>> result;
LinkedList<Integer>temp;
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
Arrays.sort(candidates);
this.candidates=candidates;
n=candidates.length;
result=new LinkedList<List<Integer>>();
temp=new LinkedList<Integer>();
help(0,target);
return result;
}
void help(int start,int left)
{
int i=start;
while(i<n)
{
int currentLeft=left-candidates[i];
if(currentLeft<0)
{
return;
}
temp.add(candidates[i]);
if(currentLeft>0)
{
int k=i+1;
if(k<n)
{
help(k,currentLeft);
}
}
else// if(currentLeft==0)
{
result.add(new LinkedList<Integer>(temp));
}
temp.removeLast();
int k=i++; while(i<n&&candidates[i]==candidates[k]) { i++; }
}
return;
}
}
while(i<n&&candidates[i]==candidates[k])
{
i++;
}
}
return;
}
}
相关文章推荐
- switch语句及其注意事项
- Linux学习19_每天一个linux命令(10):cat 命令
- APP Profiler
- 网易cc题2
- Spring4 学习笔记(1)-入门及 HelloWorld -(供自己学习)
- hdu 4831 Scenic Popularity(模拟)
- 人们为什么不使用Python3?
- Unity 分析器(仅专业版) Profiler (Pro only)
- shell自动删除一个月前的trace文件
- c#中的数据类型
- CentOS6.7 用户
- 练习2 J题 - 多项式求和
- hdoj--2509--Be the Winner(尼姆博弈)
- Java异常错误的面试题及答案
- hdoj--2509--Be the Winner(尼姆博弈)
- NVIDIA Parallel Nsight
- HDOJ--2191--悼念512汶川大地震遇难同胞——珍惜现在,感恩生活(多重背包变01背包)
- 改善Java文档的理由、建议和技巧
- Android从服务器获取.9图片失效处理
- 哈夫曼编码-->输入权重,构造哈夫曼树并输出相应哈夫曼编码(c语言)