Share my python codes, brute force


  • 0
    C
    class Solution(object):
        def findTargetSumWays(self, nums, S):
            """
            :type nums: List[int]
            :type S: int
            :rtype: int
            """
            dic = {0:1}
            for n in nums:
                newDic = {}
                for key, val in dic.iteritems():
                    newDic[key+n] = newDic.get(key+n, 0) + val
                    newDic[key-n] = newDic.get(key-n, 0) + val
                dic = newDic
            
            return dic.get(S, 0)
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.