leetcode 39. 组合总和 40. 组合总和 II
leetcode 39. 组合总和 40. 组合总和 II
组合总和
给定一个无重复元素的正整数数组 candidates 和一个正整数 target ,找出 candidates 中所有可以使数字和为目标数 target 的唯一组合。
candidates 中的数字可以无限制重复被选取。如果至少一个所选数字数量不同,则两种组合是唯一的。
对于给定的输入,保证和为 target 的唯一组合数少于 150 个。
示例 1:
输入: candidates = [2,3,6,7], target = 7
输出: [[7],[2,2,3]]
示例 2:
输入: candidates = [2,3,5], target = 8
输出: [[2,2,2,2],[2,3,3],[3,5]]
示例 3:
输入: candidates = [2], target = 1
输出: []
示例 4:
输入: candidates = [1], target = 1
输出: [[1]]
示例 5:
输入: candidates = [1], target = 2
输出: [[1,1]]
提示:
1 <= candidates.length <= 30
1 <= candidates[i] <= 200
candidate 中的每个元素都是独一无二的。
1 <= target <= 500
from typing import Listclass Solution:def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:ans = []candidates.sort()def get_sum(s, ans_arr: List[int], tar):nonlocal ans, targetif tar == 0:ans.append(ans_arr.copy())returnif tar < 0:returnfor i in range(s, len(candidates)):if candidates[i] > tar:returnans_arr.append(candidates[i])get_sum(i,ans_arr, tar - candidates[i])ans_arr.pop()get_sum(0, [], target)return ansif __name__ == '__main__':candidates = [2,3,6,7]candidates = [2,3,5]candidates = [2,7,6,3,5,1]target = 7target = 8target = 9print(Solution().combinationSum(candidates, target))
组合总和 II
给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。
candidates 中的每个数字在每个组合中只能使用一次。
注意:解集不能包含重复的组合。
示例 1:
输入: candidates = [10,1,2,7,6,1,5], target = 8,
输出:
[
[1,1,6],
[1,2,5],
[1,7],
[2,6]
]
示例 2:
输入: candidates = [2,5,2,1,2], target = 5,
输出:
[
[1,2,2],
[5]
]
提示:
1 <= candidates.length <= 100
1 <= candidates[i] <= 50
1 <= target <= 30
通过次数203,763提交次数327,393
from typing import Listclass Solution:def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:candidates.sort()arr = []length = len(candidates)if sum(candidates) < target:return []def get_sum(n, n_target, ans_arr: List[int]):nonlocal arr, candidates, target, lengthif n_target < 0:returnif n_target == 0:if ans_arr not in arr:arr.append(ans_arr.copy())returnvalid = Falsefor i in range(n, length):if valid and candidates[i] == candidates[i - 1]:continueif target - sum(ans_arr) < candidates[i]:returnans_arr.append(candidates[i])get_sum(i + 1, n_target - candidates[i], ans_arr)valid = Trueans_arr.pop()get_sum(0, target, [])return arrif __name__ == '__main__':candidates = [10, 1, 2, 7, 6, 1, 5]# candidates = [2,5,2,1,2]# candidates = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]# candidates = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,# 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,# 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]# candidates = [1, 1, 1, 1, 1, 1, 1]target = 8# target = 5# target = 27# target = 30# target = 3# print(len(candidates))candidates = [3, 1, 3, 5, 1, 1]target = 8print(Solution().combinationSum2(candidates, target))
leetcode 39. 组合总和 40. 组合总和 II相关推荐
- Leetcode 每日一题 40 组合2
题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能 ...
- 77.组合 | 40.组合总和II | 39.组合总和 | 784.字母大小写全排列
77.组合 给定两个整数 n 和 k,返回范围 [1, n] 中所有可能的 k 个数的组合. 你可以按 任何顺序 返回答案. 示例 1: 输入:n = 4, k = 2 输出: [ [2,4], ...
- Suzy找到实习了吗 Day27 | 回溯进行中:39. 组合总和,40. 组合总和 II,131.分割回文串
39. 组合总和 题目 给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 , ...
- LeetCode 40. 组合总和 II(排列组合 回溯)
1. 题目 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只 ...
- leetcode 40. 组合总和 II 思考分析
题目 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使用 ...
- LeetCode 39 组合总和
LeetCode 39 组合总和 题目描述 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合.can ...
- ii 组合总和_40. 组合总和 II
题目描述: 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只 ...
- 【Leetcode】完全背包问题-377. 组合总和 Ⅳ
[Leetcode]完全背包问题-377. 组合总和 Ⅳ 题目 给你一个由 不同 整数组成的数组 nums ,和一个目标整数 target .请你从 nums 中找出并返回总和为 target 的元素 ...
- Leetcode 40组合总数(回溯)Ⅱ41缺失的第一个正数42接雨水
维护公众号:bigsai ,回复进群加入打卡,回复bigsai分享一些学习资源! 上周第一次 LeetCode 36有效的数独&37解数独(八皇后问题) 上周第二次 LeetCode 38外观 ...
最新文章
- 如何判断飞机的年限_技术流带你鉴定前风挡玻璃更换,不再使用日期判断!
- Spring RESTFul Client – RestTemplate Example--转载
- mycat 10 分钟轻松入门
- 如何提高自己的工作效率
- PS如何制作酷炫个性字母人像海报
- 2021L3HCTF luuuuua Writeup
- 大乱斗ps4好玩吗_飞刀剑影乱作战:飞刀大乱斗ol游戏小程序,点开既玩
- 学生党无线蓝牙耳机推荐哪个,2022口碑最好的蓝牙耳机推荐
- Matlab逆向归纳法,6.完全信息动态博弈—逆向归纳法和子博弈完美均衡.ppt
- Js逆向教程-10常见代码混淆
- JSON.parse()、JSON.stringify、 parseInt()
- MAML:Model-Agnostic Meta-Learning for Fast Adaptation of Deep Networks论文精读及详解
- 国内外OTP单片机品牌大汇总
- 可以卸载什么程序来对计算机进行瘦身,爱机巧妙瘦身轻轻松松卸载软件之问与答 -电脑资料...
- 以下这段程序将单链表逆转。(单链表不带有空头结点,链表头指针是head)例如,链表 1 -> 2 -> 3 -> 4 逆转后变为 4 -> 3 -> 2 -> 1 .
- 对象的发布与逸出简单理解
- vim的常用命令使用教程
- 《PPT高手之道:六步变身职场幻灯派》一1.5 第一步谋篇之实战
- leetcode系列-226.翻转二叉树
- 盘点:拼多多有哪些值得借鉴的用户引导