【算法与数据结构】494、LeetCode目标和

文章目录

所有的LeetCode题解索引,可以看这篇文章------【算法和数据结构】LeetCode题解

一、题目

二、解法

  思路分析:本题和这道题【算法与数据结构】1049、LeetCode 最后一块石头的重量 II类似,同样可以转换成01背包问题。下面开始论述。假设添加正号的整数子集和为 p o s i t i v e positive positive,添加负号的整数子集和为 n e g a t i v e negative negative。那么我们有 p o s i t i v e − n e g a t i v e = t a r g e t , p o s i t i v e + n e g a t i v e = s u m positive - negative=target, positive + negative = sum positive−negative=target,positive+negative=sum。因此 p o s i t i v e = ( t a r g e t + s u m ) / 2 positive = (target + sum)/2 positive=(target+sum)/2,其中 s u m sum sum代表nums的和。找到了和为 p o s i t i v e positive positive的子集就找到了 n e g a t i v e negative negative。因此,问题变成了寻找和为 p o s i t i v e positive positive的子集数量。寻找和为 p o s i t i v e positive positive的子集是一个01背包问题。其中, p o s i t i v e positive positive是背包的容量,物品及其价值是 n u m s nums nums数组。 d p j dpj dpj代表填满 j j j(包括 j j j)这么大容积的包,有 d p j dpj dpj种方法。递归公式可以由 d p j − n u m s \[ i ] dpj-nums\[i] dpj−nums\[i]得出,例如只要有 n u m s i numsi numsi,那么弄成 d p j dpj dpj的方法就有 d p j − n u m s \[ i ] dpj-nums\[i] dpj−nums\[i]中,并且根据 n u m s i numsi numsi的不同会有不同的方法,所以 d p j dpj dpj应该采取累加的形式。然后 d p 0 dp0 dp0应该初始化为1(为0的话所有的 d p j dpj dpj都是0)。

  程序如下:

cpp 复制代码
class Solution {
public:
    int findTargetSumWays(vector<int>& nums, int target) {
        int sum = accumulate(nums.begin(), nums.end(), 0);
        if ((target + sum) % 2 != 0 || abs(target) > sum) return 0;
        int positive = (target + sum) / 2;
        vector<int> dp(vector<int>(positive + 1, 0));
        dp[0] = 1;
        for (int i = 0; i < nums.size(); i++) {			// 遍历物品
            for (int j = positive; j >= nums[i]; j--) {			// 遍历背包容量
                dp[j] += dp[j - nums[i]];
            }
        }
        return dp[positive];
    }
};

复杂度分析:

  • 时间复杂度: O ( m ∗ n ) O(m*n) O(m∗n), n为nums数组大小,m为背包容量。
  • 空间复杂度: O ( m ) O(m) O(m)。

三、完整代码

cpp 复制代码
# include <iostream>
# include <vector>
# include <numeric>
using namespace std;

class Solution {
public:
    int findTargetSumWays(vector<int>& nums, int target) {
        int sum = accumulate(nums.begin(), nums.end(), 0);
        if ((target + sum) % 2 != 0 || abs(target) > sum) return 0;
        int positive = (target + sum) / 2;
        vector<int> dp(vector<int>(positive + 1, 0));
        dp[0] = 1;
        for (int i = 0; i < nums.size(); i++) {			// 遍历物品
            for (int j = positive; j >= nums[i]; j--) {			// 遍历背包容量
                dp[j] += dp[j - nums[i]];
            }
        }
        return dp[positive];
    }
};

int main() {
    Solution s1;
    vector<int> nums = { 1,1,1,1,1 };
    int target = 3;
    int result = s1.findTargetSumWays(nums, target);
    cout << result << endl;
    system("pause");
    return 0;
}

end

相关推荐
小O的算法实验室7 小时前
IEEE TASE,基于MPC的多无人机协同搜索竞争群体优化方法
算法
Tbisnic8 小时前
BGE-M3 算法详解:从模型架构到三种检索方式的数学原理
算法·自然语言处理·大模型·bert·transformer·注意力机制
Brilliantwxx8 小时前
【算法从零到千】【55-58】哈希位图+常见数学运算 接口
算法
空堂与归9 小时前
用户分群找不到规律?用K-Means聚类算法自动发现数据模式
算法·机器学习·kmeans·聚类
动词ing10 小时前
【C语言】自定义函数+指针入门
c语言·开发语言·算法
重生之后端学习10 小时前
283. 移动零[简单]✅
开发语言·数据结构·算法·leetcode·职场和发展
一只小小的芙厨10 小时前
基础数论总结
笔记·学习·算法
夜不会漫长12 小时前
C++入门(1)
开发语言·c++·算法
wen_zhufeng13 小时前
IndexTTS 2.5 技术报告
人工智能·算法·机器学习
大熊背13 小时前
树莓派相机自动白平衡详解(四)
算法·树莓派·白平衡·isppipeline