@lru_cache(1000 * 1000) defrecur(self, target): ret = 0 if target == 0: return1 for x in self.nums: if x <= target: ret += self.recur(target - x) return ret
动态规划
1 2 3 4 5 6 7 8 9
classSolution: defcombinationSum4(self, nums: List[int], target: int) -> int: dp = [1] + [0] * target for i inrange(1, target + 1): for x in nums: if x <= i: dp[i] += dp[i - x] return dp[target]