LeetCode刷题(5)
来源:互联网 发布:什么是淘宝的企业店铺 编辑:程序博客网 时间:2024/06/08 07:23
Combination Sum
使用集合中的数据求特定和,组合数据无重复,但数据可重复使用
dfs算法,现对数据进行排序,每次取自身及其后的数,防止重复
res = [] candidates.sort() self.dfs(candidates, target, 0, [], res) return res def dfs(self, nums, target, index, path, res): if target < 0: return if target == 0: res.append(path) return for 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 res def dfs(self, nums, target, index, path, res): if target < 0 : return if target == 0: res.append(path) return for i in xrange(index, len(nums)): if i!=index and nums[i] == nums[i-1]: continue self.dfs(nums, target - nums[i], i + 1, path + [nums[i]], res)
比如有1,1,1,结果中只能包含两个1,那么该方案只取前两个,以避免重复。
阅读全文
0 0
- Leetcode刷题(5)
- LeetCode刷题(5)
- 【leetcode】leetcode 刷题 笔记 (不定期更新)
- leetcode刷题(Python)
- LeetCode刷题(1)
- LeetCode刷题(2)
- LeetCode刷题(3)
- LeetCode刷题(废弃)
- LeetCode刷题(FizzBuzz)
- Leetcode刷题(1)
- Leetcode刷题(2)
- Leetcode刷题(3)
- Leetcode刷题(4)
- Leetcode刷题(6)
- Leetcode刷题(7)
- Leetcode刷题(13)
- Leetcode刷题(17)
- LeetCode刷题(18)
- 2017 乌鲁木齐赛区网络赛 Half-consecutive Numbers(【规律题】)
- 跳台阶(剑指Offer 第 8 题)
- java 中Math.sqrt()使用注意事项 Type mismatch: cannot convert from double to float
- 交换排序(冒泡排序--快速排序)
- supervisord上手与配置
- LeetCode刷题(5)
- solr安装配置
- 圆的面积和周长
- C语言习题三
- 2.1.5脏读(dirtyRead)
- Java基础学习之java基本语法(4)【数组】
- jsp简介
- 间接寻址--简单操作
- Java Activiti(6)--流程变量的添加与获取(表act_ru_variable)