039 Combination Sum

039 Combination Sum

这道题就是暴力搜索, 但是有个优化 就是 Line 14 处加一个 if a[s] > target 就不需要重复搜索 这样运行时间会从 236ms 变成108ms

 1 class Solution:
 2     # @param {integer[]} candidates
 3     # @param {integer} target
 4     # @return {integer[][]}
 5     def combinationSum(self, candidates, target):
 6         candidates.sort()
 7         ans = []
 8         self.help(candidates, target, [], 0, ans)
 9         return ans
10
11     def help(self, a, target, cur, s, ans):
12         if target == 0:
13             ans.append(cur[:])
14         if target < 0 or a[s] > target:
15             return
16         while s < len(a):
17             cur.append(a[s])
18             r = self.help(a, target - a[s], cur, s, ans)
19             cur.pop()
20             s += 1

然后进一步优化 如下 line 20处 加一个 break,这样因为如果当前a[s]都已经超过target了 就没有必要继续搜索了, 这样运行时间变成 88ms

 1 class Solution:
 2     # @param {integer[]} candidates
 3     # @param {integer} target
 4     # @return {integer[][]}
 5     def combinationSum(self, candidates, target):
 6         candidates.sort()
 7         ans = []
 8         self.help(candidates, target, [], 0, ans)
 9         return ans
10
11     def help(self, a, target, cur, s, ans):
12         if target == 0:
13             ans.append(cur[:])
14         if target < 0 or a[s] > target:
15             return
16         while s < len(a):
17             cur.append(a[s])
18             r = self.help(a, target - a[s], cur, s, ans)
19             cur.pop()
20             if target - a[s] < 0:
21                 break
22             s += 1
时间: 2025-01-04 01:35:02

039 Combination Sum的相关文章

[LeetCode] 039. Combination Sum (Medium) (C++)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 039. Combination Sum (Medium) 链接: 题目:https://leetcode.com/problems/combination-sum/ 代码(github):https://github.com/illuz/leetcode 题意: 给出一些正整数集合,以及一个目标数,从集合中选择一

LeetCode 039 Combination Sum

题目要求:Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (in

【LeetCode】039. Combination Sum

题目: Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers

Java for LeetCode 039 Combination Sum

Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (including target) will

040 Combination Sum II

040 Combination Sum II 这道题是 039 Combination Sum 的延伸, 稍作改动就好 class Solution: # @param {integer[]} candidates # @param {integer} target # @return {integer[][]} def combinationSum2(self, candidates, target): candidates.sort() ans = [] self.help(candidat

LeetCode: Combination Sum II [039]

[题目] Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including target) will be

[leetcode] 040. Combination Sum II (Medium) (C++)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 040. Combination Sum II (Medium) 链接: 题目:https://leetcode.com/problems/combination-sum-ii/ 代码(github):https://github.com/illuz/leetcode 题意: 跟 039 一样(给出一些正整数集合,

leetcode笔记:Combination Sum III

一. 题目描述 Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Ensure that numbers within the set are sorted in ascending order. E

leetcode笔记:Combination Sum II

一. 题目描述 Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C once number of times. Note: ? All numbers (including target)