LeetCode刷题(5)
Combination Sum
使用集合中的数据求特定和,组合数据无重复,但数据可重复使用
dfs算法,现对数据进行排序,每次取自身及其后的数,防止重复
res = []candidates.sort()self.dfs(candidates, target, 0, [], res)return resdef dfs(self, nums, target, index, path, res):if target < 0:returnif target == 0:res.append(path)returnfor i in xrange(index, len(nums)):self.dfs(nums, target-nums[i], i, path+[nums[i]], res)
Combination Sum II
数据不能重复使用
res = []candidates.sort()self.dfs(candidates, target, 0, [], res)return resdef dfs(self, nums, target, index, path, res):if target < 0 :return if target == 0:res.append(path)returnfor i in xrange(index, len(nums)):if i!=index and nums[i] == nums[i-1]:continueself.dfs(nums, target - nums[i], i + 1, path + [nums[i]], res)
比如有1,1,1,结果中只能包含两个1,那么该方案只取前两个,以避免重复。
LeetCode刷题(5)相关推荐
- LeetCode刷题记录15——21. Merge Two Sorted Lists(easy)
LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) 目录 LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) ...
- LeetCode刷题记录14——257. Binary Tree Paths(easy)
LeetCode刷题记录14--257. Binary Tree Paths(easy) 目录 前言 题目 语言 思路 源码 后记 前言 数据结构感觉理论简单,实践起来很困难. 题目 给定一个二叉树, ...
- LeetCode刷题记录13——705. Design HashSet(easy)
LeetCode刷题记录13--705. Design HashSet(easy) 目录 LeetCode刷题记录13--705. Design HashSet(easy) 前言 题目 语言 思路 源 ...
- LeetCode刷题记录12——232. Implement Queue using Stacks(easy)
LeetCode刷题记录12--232. Implement Queue using Stacks(easy) 目录 LeetCode刷题记录12--232. Implement Queue usin ...
- LeetCode刷题记录11——290. Word Pattern(easy)
LeetCode刷题记录11--290. Word Pattern(easy) 目录 LeetCode刷题记录11--290. Word Pattern(easy) 题目 语言 思路 源码 后记 题目 ...
- LeetCode刷题记录10——434. Number of Segments in a String(easy)
LeetCode刷题记录10--434. Number of Segments in a String(easy) 目录 LeetCode刷题记录9--434. Number of Segments ...
- LeetCode刷题记录9——58. Length of Last Word(easy)
LeetCode刷题记录9--58. Length of Last Word(easy) 目录 LeetCode刷题记录9--58. Length of Last Word(easy) 题目 语言 思 ...
- LeetCode刷题记录8——605. Can Place Flowers(easy)
LeetCode刷题记录8--605. Can Place Flowers(easy) 目录 LeetCode刷题记录8--605. Can Place Flowers(easy) 题目 语言 思路 ...
- LeetCode刷题记录7——824. Goat Latin(easy)
LeetCode刷题记录7--824. Goat Latin(easy) 目录 LeetCode刷题记录7--824. Goat Latin(easy) 题目 语言 思路 后记 题目 题目需要将一个输 ...
- LeetCode刷题记录6——696. Count Binary Substrings(easy)
LeetCode刷题记录6--696. Count Binary Substrings(easy) 目录 LeetCode刷题记录6--696. Count Binary Substrings(eas ...
最新文章
- linux连接小米随身wifi密码忘记了,小米wifi管理员密码忘记了怎么办?
- PCLVisualizer可视化类
- 我在交大的数学建模经验!
- c语言中的extern_7.6 C语言内部函数和外部函数
- 主键策略UUID升级
- 北大韦神透露现状:自己课讲得不太好,中期学生退课后就剩下5、6个人...
- javaScript事件(二)事件处理程序
- 算法------长度最小的子数组
- CentOS下tftp 安装配置使用
- bzoj3786: 星系探索
- Java进阶高级程序员必备:深入浅出springboot+深入Java虚拟机
- 计算机二级c语言题库缩印,计算机二级C语言上机题库(可缩印做考试小抄资料)...
- 实验楼python挑战答案_python基础知识
- 推荐两个Magento做的中文网站 GAP和佰草集
- 虚拟机ping不通主机与外网的解决方法
- 冰点还原忘记密码怎样删除?
- 5年Java面试阿里P6经验总结
- OPENSTREETMAP电力数据的情况
- ddr走线教程_Xilinx FPGA的DDR走线规则
- Mysql - 知识图谱总览