LeetCode 377: Combination Sum IV
Posted keepshuatishuati
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 377: Combination Sum IV相关的知识,希望对你有一定的参考价值。
class Solution { public int combinationSum4(int[] nums, int target) { if (nums.length == 0) { return 0; } int[] dp = new int[target + 1]; dp[0] = 1;; for (int i = 1; i < dp.length; i++) { for (int j = 0; j < nums.length; j++) { if (i - nums[j] >= 0) { dp[i] += dp[i - nums[j]]; } } } return dp[target]; } }
It‘s like the packing problem.
Brute force:
class Solution { public int combinationSum4(int[] nums, int target) { if (target < 0) { return 0; } if (target == 0) { return 1; } int result = 0; for(int num : nums) { result += combinationSum4(nums, target - num); } return result; } }
以上是关于LeetCode 377: Combination Sum IV的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode 377. Combination Sum IV
[leetcode-377-Combination Sum IV]
LeetCode 377: Combination Sum IV