Python dictionary + stack long code, just for fun


  • 0
    P

    I didn't notice it had already been in ascending order ( now I do realize it's highlighted though ^_^|||). And I didn't want to sort, so, post this long code just for fun!

    class Solution(object):
       def pathSum(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            dict = {}
            stack, res = [], 0
            for num in nums:
            	d, p, v = num//100, num%100//10, num%10
            	dict[ (d,p) ] = v
            	if (d,p) == (1,1):
            		stack.append( (d,p,v) )
            
            while stack:
            	cd, cp, cv = stack.pop()
            	if (cd+1, cp*2-1) not in dict and (cd+1, cp*2) not in dict:
            		res += cv
            		continue
            	if (cd+1, cp*2-1) in dict:
            		stack.append( (cd+1, cp*2-1, cv + dict[(cd+1, cp*2-1)]) )
            	if (cd+1, cp*2) in dict:
            		stack.append( (cd+1, cp*2, cv + dict[(cd+1, cp*2)]) )
    
    
            return res
    

Log in to reply
 

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