Re: [闲聊] 每日LeetCode

楼主: Rushia (みけねこ的鼻屎)   2023-09-09 12:07:25
https://leetcode.com/problems/combination-sum-iv/description/
377. Combination Sum IV
给你一个只包含不重复正整数的阵列 nums,和一个数字 target,求出只使用 nums 里面
的数字共有几种方式可以组成 target。
Example 1:
Input: nums = [1,2,3], target = 4
Output: 7
Explanation:
The possible combination ways are:
(1, 1, 1, 1)
(1, 1, 2)
(1, 2, 1)
(1, 3)
(2, 1, 1)
(2, 2)
(3, 1)
Note that different sequences are counted as different combinations.
Example 2:
Input: nums = [9], target = 3
Output: 0
思路:
1.NSum加上动态规划,我们可以用 target - num 检查是否可以加上当前数字得到和,
把所有的总和加总即可。
2.因为有很多重复计算所以用 memoization 来暂存已经算过的结果。
Java Code:
作者: JerryChungYC (JerryChung)   2023-09-09 12:39:00
大师

Links booklink

Contact Us: admin [ a t ] ucptt.com