leetcode39
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的更多相关文章
- 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 ...
- LeetCode39 Combination Sum
题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C ...
- [Swift]LeetCode39. 组合总和 | Combination Sum
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...
- LeetCode39.组合总和 JavaScript
给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限制重复被选 ...
- Leetcode39.Combination Sum组合总和
给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限制重复被选 ...
- leetcode39 组合总和
这道题想到的就是dfs,在累加的和大于或等于target时到达递归树的终点. 代码如下: class Solution { public: vector<vector<int>> ...
- LeetCode一句话题解
深度优先搜索 人生经验 1. 需要输出所有解.并由于元素集有重复元素,要求返回的结果需要去重的情况,可考虑使用值对应数量的map,然后分别考虑依次取不同数量该值的可能. LeetCode39 题目:给 ...
- leetcode 日常清单
a:excellent几乎一次ac或只有点小bug很快解决:半年后再重刷: b:经过艰难的debug和磕磕绊绊或者看了小提示才刷出来: c:经过艰难的debug没做出来,看答案刷的: 艾宾浩斯遗忘曲线 ...
- LeetCode通关:连刷十四题,回溯算法完全攻略
刷题路线:https://github.com/youngyangyang04/leetcode-master 大家好,我是被算法题虐到泪流满面的老三,只能靠发发文章给自己打气! 这一节,我们来看看回 ...
随机推荐
- 开始一个django项目的流程
1.明确开发站点的主题,(即此站点的作用), 确定站点的各种功能,需求. 2.优先设计数据库. 数据库的设计要合理,不能想当然的设计,最好能够以表格的形式展现出来,避免以后遗忘,也避免内容的重复. 3 ...
- ela的UNASSIGNED索引修复
1.查找UNASSIGNED未分片的索引: #curl -s "http://localhost:9200/_cat/shards" -u username:passwd | gr ...
- linux之创建用户
用户 useradd xxx 创建用户 默认是普通用户 useradd -u666 web 创建新用户 设置id号 groupadd -g 777 ...
- OpenCV实现彩色图像轮廓 换背景颜色
转摘请注明:https://i.cnblogs.com/EditPosts.aspx?opt=1 有时候我们需要不一样颜色的证件照,下面就用OpenCV来实现证件照的蓝底.红底等换颜色: 代码如下: ...
- Javascript 2.8
声明函数声明参数 function multiply(A,B,...N){}; 用return可以返回一个值/字符串/数组/布尔值 变量命名的Camel记号:从第二个单词开始把每个单词的首字母大写,其 ...
- 使用outflux 导入influxdb 的数据到timescaledb
influxdb 以及timescaledb 都是不错的时序数据库,timescaledb 团队提供了直接从influxdb 导入 环境准备 docker-compose 文件 version: &q ...
- Linux 文件查看,文件夹切换,权限查看
当前用户只操作当前用户目录 1. 输入终端显示内容: 用户 @ 系统 : 路径信息 $ $ 表示普通用户 家目录 # 表示超级用户 家目录 [sudo -i ] 使用root用户 : 使用 ...
- 一、Ansible安装
1:安装ansible ,选择的是yum 安装,简单,ansible的管理端必须是Linux系统 YUM是一个shell前端软件包管理器.基于RPM包管理,能够从指定的服务器自动下载RPM ...
- 删除iis日志(deliislogs.vbs)
'path 目录 'ext 文件扩展名'expiredDays 保留多少天以内的文件Sub LogCleaner(path,ext,expiredDays) On Error Resume Next ...
- Windows系统中监控文件复制操作的几种方式
http://blog.sina.com.cn/s/blog_4596beaa0100lp4y.html 1. ICopyHook 作用: 监视文件夹和打印机移动,删除, 重命名, 复制操作. 可以得 ...