Leetcode - 494. Target Sum

Example 1:

Input:nums is [1, 1, 1, 1, 1], S is 3.Output:5Explanation:-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 = 3There are 5 ways to assign symbols to make the sum of nums be target 3.


simple solution:

(DFS)

there are two trees in the problem, one is the tree start with positive, another oen is the tree start with negative tree.

for example 

[1,1,1]

------------------

T1(left for sub , right for add)

         1

     0         2          

-1      1   1       3

T2

             -1

       -2          0

-3         -1   -1       1


then we add the total of the result of these two trees


WHILE, python will have time limit exceed problem .. you know , python....

this is the DP solution, which is extremely fast 

https://discuss.leetcode.com/topic/76205/python-dp/7


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,789评论 0 33
  • 那时候,天,真的很蓝; 那时候,水,真的很清; 那时候,渔舟,真的很安详; 那时候,老农,真的很热情; 那时候,鸟...
    向左转全是山阅读 301评论 0 0
  • 从2016-8-22 打算开始学习sketch一来,就一共买了三本书,记录总归让我这段时间的付出有落实到实物上。刚...
    刘常晔阅读 385评论 0 0
  • 1.基本 2.绑定方法 3.直接传参 3.有时候我们需要获取event 4.event 上的方法?? .stop....
    lmem阅读 2,226评论 0 1
  • 1.切片切片可以取list、tuple、string的元素python语言中把字符串看做一个tuple,因此可以通...
    JEZAU阅读 459评论 1 2