public class Solution
{
List<IList<int>> list = new List<IList<int>>();//全部记录
List<int> records = new List<int>();//一条记录
bool bk = false;
private void BackTrack(List<int> candidates, int target, int sum)
{
if (sum == target)
{
int[] temp = new int[records.Count];
records.CopyTo(temp);
bool same = false;
foreach (var l in list)
{
if (l.Count == temp.Length)
{
var l1 = l.OrderBy(x => x).ToList();
var l2 = temp.OrderBy(x => x).ToList();
var samecount = ;
for (int x = ; x < l1.Count; x++)
{
if (l1[x] == l2[x])
{
samecount++;
}
}
if (samecount == l.Count)
{
same = true;
break;
}
}
}
if (!same)
{
list.Add(temp.ToList());
}
bk = true;
return;
}
else if (sum < target)
{
bk = true;
for (int position = ; position < candidates.Count; position++)
{
var cur = candidates[position];
sum += cur;
records.Add(cur);
BackTrack(candidates, target, sum);
//回溯
records.RemoveAt(records.Count - );
sum -= cur;
if (bk)
{
bk = false;
break;
}
}
}
else
{
bk = true;
return;
}
} public IList<IList<int>> CombinationSum(int[] candidates, int target)
{
var can = candidates.OrderBy(x => x).ToList();
BackTrack(can, target, );
return list;
}
}

补充一个python的实现,写的要简单的多了:

 class Solution:
def dfs(self,candidates,target,index,path,l):
if target < 0:
return
elif target == 0:
l.append(path)
return
for i in range(index,len(candidates)):
self.dfs(candidates,target-candidates[i],i,path+[candidates[i]],l)
return def combinationSum(self, candidates: 'List[int]', target: 'int') -> 'List[List[int]]':
l = list()
path = []
candidates.sort()
self.dfs(candidates,target,0,path,l)
return l

leetcode39的更多相关文章

  1. LeetCode39/40/22/77/17/401/78/51/46/47/79 11道回溯题(Backtracking)

    LeetCode 39 class Solution { public: void dfs(int dep, int maxDep, vector<int>& cand, int ...

  2. LeetCode39 Combination Sum

    题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C  ...

  3. [Swift]LeetCode39. 组合总和 | Combination Sum

    Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...

  4. LeetCode39.组合总和 JavaScript

    给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限制重复被选 ...

  5. Leetcode39.Combination Sum组合总和

    给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限制重复被选 ...

  6. leetcode39 组合总和

    这道题想到的就是dfs,在累加的和大于或等于target时到达递归树的终点. 代码如下: class Solution { public: vector<vector<int>> ...

  7. LeetCode一句话题解

    深度优先搜索 人生经验 1. 需要输出所有解.并由于元素集有重复元素,要求返回的结果需要去重的情况,可考虑使用值对应数量的map,然后分别考虑依次取不同数量该值的可能. LeetCode39 题目:给 ...

  8. leetcode 日常清单

    a:excellent几乎一次ac或只有点小bug很快解决:半年后再重刷: b:经过艰难的debug和磕磕绊绊或者看了小提示才刷出来: c:经过艰难的debug没做出来,看答案刷的: 艾宾浩斯遗忘曲线 ...

  9. LeetCode通关:连刷十四题,回溯算法完全攻略

    刷题路线:https://github.com/youngyangyang04/leetcode-master 大家好,我是被算法题虐到泪流满面的老三,只能靠发发文章给自己打气! 这一节,我们来看看回 ...

随机推荐

  1. 2D过渡模块的其他属性

    官网上关于过渡属性的值: 属性 描述 CSS transition 简写属性,用于在一个属性中设置四个过渡属性. 3 transition-property 规定应用过渡的 CSS 属性的名称. 3 ...

  2. 数据库行转列、列转行,pivot透视多列

    这就是典型的行转列问题. 首先说下最简单的思路  用union all select year,sum(m1) m1,sum(m2) m2,sum(m3) m3,sum(m4) m4 from ( s ...

  3. 2019-04-10-day029-粘包处理

    内容回顾 osi五层协议 不是真实存在的,只是抽象出来的模型 应用层 传输层 TCP/UDP TCP :全双工,可靠的,面向连接的,速度慢,对数据大小没有限制 建立连接 :三次握手 SYN ACK 断 ...

  4. jenkins 持续集成iOS开发

    持续集成即Continuous Integration,简称CI 1,安装jenkins,brew install jenkins 2,在浏览器输入localhost:8080会出现一个网页,要求输入 ...

  5. 【oracle入门】Oracle数据库11g企业版主要优点

    高可靠性.能够尽可能地放置服务器故障.站点故障和人为错误的发生. 高安全信息.可以利用行级安全性.细粒度审计.透明的数据加密和数据的全面会议确保数据安全和遵守法规. 更好的数据管理.轻松管理最大型数据 ...

  6. Codeforces1099F. Cookies【DP】【线段树】【贪心】【博弈】【沙比提(这是啥算法)】

    LINK 题目大意 给你一棵树,每个节点上有一堆饼干 并且告诉你在每个节点上吃饼干吃一块需要多少时间 然后告诉你经过一条边的时间 总时间是T 两个人轮流进行,一个人向下选择子节点行走或者结束游戏并向上 ...

  7. xamarin C# 安卓实现 ListView 放大缩小

    翻译自java示例https://raw.githubusercontent.com/Xjasz/AndroidZoomableViewGroup/master/ZoomListView.java u ...

  8. [转]ANTS Performance Profiler和ANTS Memory Profiler 使用

    .NET性能调优之一:ANTS Performance Profiler的使用   .NET性能调优系列文章 系列文章索引 .NET性能调优之一:ANTS Performance Profiler的使 ...

  9. mysql ibdata1

    ibdata1是什么? Mysql ibdata1即Innodb data1缩写,是innodb引擎的表空间,用于存放 数据字典Data dictionary:  只读的表,存储对象的相关信息,如占用 ...

  10. micrometer自定义metrics

    micrometer提供了基于Java的monitor facade,其与springboot应用和prometheus的集成方式如下图展示 上图中展示的很清楚,应用通过micrometer采集和暴露 ...