Python Simple Solution


  • 0
    J
    class Solution(object):
        def pathSum(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            node = [0]*(2**4)
            res = 0
            for i in range(len(nums)):
                level = nums[i]//100
                pos = (nums[i]//10)%10
                
                #reindex
                ind = (1<<(level-1)) + pos - 1
                node[ind] = nums[i]%10
                
                prelevel = nums[i-1]//100
                prepos = (nums[i-1]//10)%10
                #chk whether the node has a previous brother
                if pos % 2 == 0 and level == prelevel and pos == prepos + 1:
                    while ind > 0:
                        res += node[ind]
                        ind //= 2
                else:
                    res += node[ind]
            return res
    

Log in to reply
 

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