494. 目标和

自主完成,然后看了提示为动态规划,然后就是没有写状态转移方程,花了很多时间调试

查看代码
class Solution:
    def findTargetSumWays(self, nums: List[int], target: int) -> int:
        m={}
        temp=0
        m_1={}
        m_1[0]=1
        for i in nums:
            for k in  list(m_1.keys()):
                temp = k
                temp+=i
                if temp not in m:
                    m[temp]=m_1[k]
                else:
                     m[temp]+=m_1[k]
                temp=temp-i-i
                if temp not in m:
                    m[temp]=m_1[k]
                else:
                     m[temp]+=m_1[k]
            print(list(m.keys()),list(m.values()))
            m_1=m
            m={}
        if target not in m_1:
            return 0
        return m_1[target]
posted @ 2022-01-09 16:42  jozon  阅读(21)  评论(0)    收藏  举报