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. CodeForces - 589B(暴力+排序)

    Dasha decided to bake a big and tasty layer cake. In order to do that she went shopping and bought n ...

  2. Spring 源码学习(1)—— 容器的基本实现

    最近在读Spring的源码,参考的是郝佳的<Spring源码深度解析>,这里把一些学习心得分享一下,总结的地方可能还有一些不完善,希望大家指教 IoC(控制反转)是Spring的特性之一, ...

  3. java面向对象编程(四)--类变量、类方法

    1.什么是类变量? 类变量是该类的所有对象共享的变量,任何一个该类的对象去访问它时,取到的都是相同的值,同样任何一个该类的对象去修改它时,修改的也是同一个变量. 如何定义类变量? 定义语法:     ...

  4. js获取table中的列的数字的和

    function getTdValue(a) { var tableId = document.getElementById("tab"); var num; for(var i= ...

  5. .NET并行计算和并发6-获取线程池的最大可用线程数

    using System; using System.IO; using System.Security.Permissions; using System.Threading; class Test ...

  6. dos脚本

    关于dos命令行脚本编写   dos常用命令另查 开始之前先简单说明下cmd文件和bat文件的区别:在本质上两者没有区别,都是简单的文本编码方式,都可以用记事本创建.编辑和查看.两者所用的命令行代码也 ...

  7. Centos系统下 Gitolite安装与相关配置(git权限控制软件)

     确保已经安装了最新版的git服务Git源码安装 Linux指定安装目录,并且创建了 用户名为git 的账户 修改.gitolite.rc #设置gitolite管理的仓库目录 GL_REPO_BAS ...

  8. vue2.0 父子组件通信 兄弟组件通信

    父组件是通过props属性给子组件通信的来看下代码: 父组件: <parent> <child :child-com="content"></chil ...

  9. css布局与文档流的关系之float(浮动)

    所谓文档流,指元素在排版布局的过程中,元素会自动从左到右,从上到下的流式排列.脱离文档流呢,就是元素打乱了这个排列,或是从排版中拿走. 说到文档流呢,我们先来说一下元素,每个元素呢,都有display ...

  10. Socket基础之-启动异步服务侦听

    Socket网络编程第一篇: 本文主要是以代码为主. .NET技术交流群 199281001 .欢迎加入 1 //负责监听的套接字 private Socket socketServer; //通知一 ...