当前位置:网站首页>Brainstorm: Goals and
Brainstorm: Goals and
2022-08-10 15:25:00 【InfoQ】
title
Input: nums: [1, 1, 1, 1, 1], S: 3
Output: 5
- The array is not empty and the length will not exceed 20.
- The initial array sum will not exceed 1000.
- The final result returned is guaranteed to be stored as a 32-bit integer.
Solution ideas
Code Implementation
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];
}
}
Last
- Time complexity: O(n × m), n is a positive number, m is the backpack capacity
- Space complexity: O(m), m is the backpack capacity
边栏推荐
猜你喜欢
fastposter v2.9.1 程序员必备海报生成器
Meaning and names of 12 nautical miles, 24 nautical miles and 200 nautical miles
Azure IoT Partner Technology Empowerment Workshop: IoT Dev Hack
Introduction to program debugging and its use
Understanding_Data_Types_in_Go
物资采购小程序开发制作功能介绍
PyTorch multi-machine multi-card training: DDP combat and skills
Analysys and the Alliance of Small and Medium Banks jointly released the Hainan Digital Economy Index, so stay tuned!
Flask框架——基于Celery的后台任务
Appium进行APP自动化测试
随机推荐
JS入门到精通完整版
Problem solving-->Online OJ (19)
systemui shield notification bar
BFT机器人带你走进智慧生活 ——探索遨博机器人i系列的多种应用
TestLink Export Use Case Transformation Tool
“蔚来杯“2022牛客暑期多校训练营7
MySQL Principle and Optimization: Update Optimization
SWIG教程《二》
PyTorch 多机多卡训练:DDP 实战与技巧
CSP-J1 CSP-S1 初赛 第1轮(2022.08.09)
高薪程序员&面试题精讲系列135之你对分布式是怎么理解的?CAP理论你知道吗?
TestLink导出用例转换工具
const修饰的指针变量(详解)
E. Cross Swapping (and check out deformation/good questions)
epoll学习:思考一种高性能的服务器处理框架
网络初识(二)
华为云DevCloud获信通院首批云原生技术架构成熟度评估的最高级认证
王学岗————直播推流(软便)03x264集成与camera推流
NFT digital collection development issue - digital collection platform
[Letter from Wu Enda] The development of reinforcement learning!