首页 > 其他分享 >[LeetCode][494]target-sum

[LeetCode][494]target-sum

时间:2023-09-01 11:47:31浏览次数:30  
标签:target nums int sum cache put 494 cache0 LeetCode

Content

You are given an integer array nums and an integer target.

You want to build an expression out of nums by adding one of the symbols '+' and '-' before each integer in nums and then concatenate all the integers.

  • For example, if nums = [2, 1], you can add a '+' before 2 and a '-' before 1 and concatenate them to build the expression "+2-1".

Return the number of different expressions that you can build, which evaluates to target.

 

Example 1:

Input: nums = [1,1,1,1,1], target = 3
Output: 5
Explanation: There are 5 ways to assign symbols to make the sum of nums be target 3.
-1 + 1 + 1 + 1 + 1 = 3
+1 - 1 + 1 + 1 + 1 = 3
+1 + 1 - 1 + 1 + 1 = 3
+1 + 1 + 1 - 1 + 1 = 3
+1 + 1 + 1 + 1 - 1 = 3

Example 2:

Input: nums = [1], target = 1
Output: 1

 

Constraints:

  • 1 <= nums.length <= 20
  • 0 <= nums[i] <= 1000
  • 0 <= sum(nums[i]) <= 1000
  • -1000 <= target <= 1000
Related Topics
  • 数组
  • 动态规划
  • 回溯

  • 标签:target,nums,int,sum,cache,put,494,cache0,LeetCode
    From: https://www.cnblogs.com/shea24/p/17671432.html
  • 相关文章

    • leetcode & c++多线程刷题日志
      1.按序打印按序打印解法互斥锁classFoo{mutexmtx1,mtx2;public:Foo(){mtx1.lock(),mtx2.lock();}voidfirst(function<void()>printFirst){printFirst();mtx1.unlock();}voidsecond(function<voi......
    • 2023.08.29T3 - summer - solution
      summerProblemSolution挺好的题,题解也写得很清楚,因此我不过是把题解抄一遍。赛时打了\(40\)分,然后挂了\(20\)分,因为不会前缀和(这个人暴力求区间和,铸币吧)。前\(40\)分就是记忆化搜索+单调栈:首先考察对于一个确定的序列,如何求出一段区间的权值和。那么首先就要知道如......
    • Leetcode刷题笔记——单调性
      单调性单调性是数学中使用的一种常见性质,通常用于描述函数,在高等数学中的定义常常为:设函数f(x)在区间I上有定义,如果对于I上的任意两个数x1和x2,当x1<x2时,有f(x1)<f(x2)(或者f(x1)>f(x2)),则称函数f(x)在区间I上是单调递增的(或者单调递减的)。例如如下图像就是两个单调函数。利用单......
    • Leetcode 剑指Offer 05. 替换空格(Ti huan kong ge lcof)
      题目链接请实现一个函数,把字符串s中的每个空格替换成"%20"。示例1:输入:s="Wearehappy."输出:"We%20are%20happy."提示:0<=s的长度<=10000思路直接提交returns.replace("","%20"),常用方法信手拈来可不是每个人都能做到的(笑我的思路是首先定义一个leng......
    • [LeetCode][309]best-time-to-buy-and-sell-stock-with-cooldown
      ContentYouaregivenanarraypriceswhereprices[i]isthepriceofagivenstockontheithday.Findthemaximumprofityoucanachieve.Youmaycompleteasmanytransactionsasyoulike(i.e.,buyoneandselloneshareofthestockmultipletimes)w......
    • Android开发|备战金九银十,LeetCode高频面试题合集
      金九银十来了,你准备好备战了么!而最高效的准备方式,不外乎刷题、刷题、刷题。刷题就不得不提LeetCode了~俗话说的好:LeetCode刷不好,一面都过不了。所以,今天就将一些LeetCode大厂高频面试题整理成合集分享给大家,希望能助大家一臂之力~有需要的小伙伴,可以点击下方课程链接详细了解!!!h......
    • RISC-V 中国峰会 | OpenMPL引人注目,RISC-V Summit China 2023圆满落幕
      RISC-V中国峰会圆满落幕     2023年8月25日,为期三天的RISC-V中国峰会(RISC-VSummitChina2023)圆满落幕。本届峰会以“RISC-V生态共建”为主题,结合当下全球新形势,把握全球新时机,呈现RISC-V全球新观点、新趋势。吸引了超过百家企业及研究机构、开源技术社区参与交流,近百家媒......
    • 【CF1519D】Maximum Sum of Products
      #include<bits/stdc++.h>usingnamespacestd;typedeflonglongll;lln,a[5000+10],b[5000+10],abpre[5000+10],absuf[5000+10],ans;intmain(){ cin>>n; for(lli=1;i<=n;i++)cin>>a[i]; for(lli=1;i<=n;i++)cin>>b[i]; for(......
    • shasum
      Terminal$sha256sum-tEnterthetextandpressctrl+dwhenyouarefinished.Algorithm$shasum-a256file.txtChecksha256sum-cfile.txtInAction$echo"What>">file1.txt$echo"Where">file2.txt$echo"wh......
    • [LeetCode][300]longest-increasing-subsequence
      ContentGivenanintegerarraynums,returnthelengthofthelongeststrictlyincreasingsubsequence. Example1:Input:nums=[10,9,2,5,3,7,101,18]Output:4Explanation:Thelongestincreasingsubsequenceis[2,3,7,101],thereforethelengthis4.E......