【算法与数据结构】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 ] dp[j] dp[j]代表填满 j j j(包括 j j j)这么大容积的包,有 d p [ j ] dp[j] dp[j]种方法。递归公式可以由 d p [ j − n u m s [ i ] ] dp[j-nums[i]] dp[j−nums[i]]得出,例如只要有 n u m s [ i ] nums[i] nums[i],那么弄成 d p [ j ] dp[j] dp[j]的方法就有 d p [ j − n u m s [ i ] ] dp[j-nums[i]] dp[j−nums[i]]中,并且根据 n u m s [ i ] nums[i] nums[i]的不同会有不同的方法,所以 d p [ j ] dp[j] dp[j]应该采取累加的形式。然后 d p [ 0 ] dp[0] dp[0]应该初始化为1(为0的话所有的 d p [ j ] dp[j] dp[j]都是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

相关推荐
_OP_CHEN7 小时前
【算法基础篇】(五十七)线性代数之矩阵乘法从入门到实战:手撕模板 + 真题详解
线性代数·算法·矩阵·蓝桥杯·c/c++·矩阵乘法·acm/icpc
天天爱吃肉82187 小时前
【跨界封神|周杰伦×王传福(陶晶莹主持):音乐创作与新能源NVH测试,底层逻辑竟完全同源!(新人必看入行指南)】
python·嵌入式硬件·算法·汽车
im_AMBER8 小时前
Leetcode 114 链表中的下一个更大节点 | 删除排序链表中的重复元素 II
算法·leetcode
xhbaitxl8 小时前
算法学习day38-动态规划
学习·算法·动态规划
多恩Stone8 小时前
【3D AICG 系列-6】OmniPart 训练流程梳理
人工智能·pytorch·算法·3d·aigc
历程里程碑8 小时前
普通数组----轮转数组
java·数据结构·c++·算法·spring·leetcode·eclipse
pp起床8 小时前
贪心算法 | part02
算法·leetcode·贪心算法
sin_hielo8 小时前
leetcode 1653
数据结构·算法·leetcode
2501_901147838 小时前
面试必看:优势洗牌
笔记·学习·算法·面试·职场和发展
YuTaoShao8 小时前
【LeetCode 每日一题】3634. 使数组平衡的最少移除数目——(解法二)排序 + 二分查找
数据结构·算法·leetcode