当前位置:网站首页>头脑风暴:目标和
头脑风暴:目标和
2022-08-10 14:51:00 【InfoQ】
题目
输入:nums: [1, 1, 1, 1, 1], S: 3
输出:5
- 数组非空,且长度不会超过 20 。
- 初始的数组的和不会超过 1000 。
- 保证返回的最终结果能被 32 位整数存下。
解题思路
代码实现
class Solution {
public int findTargetSumWays(int[] nums, int target) {
int sum = 0;
for (int i = 0; i < nums.length; i++) sum += nums[i];
if ((target + sum) % 2 != 0) return 0;
int size = (target + sum) / 2;
if(size < 0) size = -size;
int[] dp = new int[size + 1];
dp[0] = 1;
for (int i = 0; i < nums.length; i++) {
for (int j = size; j >= nums[i]; j--) {
dp[j] += dp[j - nums[i]];
}
}
return dp[size];
}
}
最后
- 时间复杂度:O(n × m),n为正数个数,m为背包容量
- 空间复杂度:O(m),m为背包容量
边栏推荐
- Parallels 将扩展桌面平台产品,以进一步改善在 Mac 上运行 Windows 的用户体验和工作效率
- List集合
- SWIG tutorial "two"
- pytest框架优化
- Zhaoqi Technology Innovation High-level Talent Entrepreneurship Competition Platform
- Redis -- Nosql
- usb转rs485测试软件,usb转rs485「建议收藏」
- systemui状态栏添加新图标
- Digital Collection Platform System Development Practice
- BFT机器人带你走进智慧生活 ——探索遨博机器人i系列的多种应用
猜你喜欢

Rich Dad Poor Dad Reading Notes

Appium for APP automation testing

Alibaba的秒杀系统—千亿级并发设计手册上线了

软件测试用例篇

Based on Azuki Series: NFT Valuation Analysis Framework "DRIC"

fatal error C1083 无法打开包括文件'io.h' No such file

Do not access Object.prototype method ‘hasOwnProperty‘ from target object....

易基因|深度综述:m6A RNA甲基化在大脑发育和疾病中的表观转录调控作用

产品使用说明书小程序开发制作说明

Understanding_Data_Types_in_Go
随机推荐
阿里五位MySQL封神大佬耗17个月总结出53章性能优化法则
Introduction to the Internet (2)
Zijin Example
Understanding_Data_Types_in_Go
容器化 | 在 S3 实现定时备份
Mysql statement analysis, storage engine, index optimization, etc.
pm2 static file service
使用Uiautomator2进行APP自动化测试
高薪程序员&面试题精讲系列135之你对分布式是怎么理解的?CAP理论你知道吗?
2022-08-10 Daily: Swin Transformer author Cao Yue joins Zhiyuan to carry out research on basic vision models
强意识 压责任 安全培训筑牢生产屏障
Flask框架——基于Celery的后台任务
BCG库简介
基于 Azuki 系列:NFT估值分析框架“DRIC”
物资采购小程序开发制作功能介绍
丁香园
pytest框架优化
Redis -- Nosql
Meaning and names of 12 nautical miles, 24 nautical miles and 200 nautical miles
Pagoda panel open Redis to specify the network machine