1. 程式人生 > >leetcode 解答 in python 3.6

leetcode 解答 in python 3.6

自己做的 剛開始做 寫的有點慢

第一題

class Solution(object):
    def twoSum(nums, target):
        count=0
        for a in range(0,len(nums)-1):
            count+=1
            for b in range(count,len(nums)):
                if(nums[a]+nums[b]==target):
                    return [a,b]
        print('No Result')
        return None

    print(twoSum(nums=[2,3,4],target=6))

第二題

class Solution(object):
    def addTwoNumbers(self, l1, l2):
        res = {}
        finalres = []
        up = 0
        w = 0
        while l1 != None and l2 != None:
            sum = l1.val + l2.val
            if (sum > 9):
                up = 1
                res[str(w + 1)] = 1
                if (res.get(str(w)) != None):
                    res[str(w)] = res.get(str(w)) + (sum % 10)
                else:
                    res[str(w)] = sum % 10
                w += 1
                l1 = l1.next
                l2 = l2.next
            else:
                up = 0
                if (res.get(str(w)) != None):
                    res[str(w)] = res[str(w)] + sum
                    if (res.get(str(w)) > 9):
                        res[str(w)] = res[str(w)] - 10
                        res[str(w + 1)] = 1
                else:
                    res[str(w)] = sum
                w += 1
                l1 = l1.next
                l2 = l2.next
        while l1 != None:
            if (res.get(str(w)) != None):
                res[str(w)] = res[str(w)] + l1.val
                if (res.get(str(w)) > 9):
                    res[str(w)] = res[str(w)] - 10
                    res[str(w + 1)] = 1
            else:
                res[str(w)] = l1.val
            w += 1
            l1 = l1.next
        while l2 != None:
            if (res.get(str(w)) != None):
                res[str(w)] = res[str(w)] + l2.val
                if (res.get(str(w)) > 9):
                    res[str(w)] = res[str(w)] - 10
                    res[str(w + 1)] = 1
            else:
                res[str(w)] = l2.val
            w += 1
            l2 = l2.next

        for _ in range(w + 1):
            if (res.get(str(_)) != None):
                finalres.append(res.get(str(_)))

        return finalres

第二十題

# 給定一個只包括 '(',')','{','}','[',']' 的字串,判斷字串是否有效。
# #
# # 有效字串需滿足:
# #
# # 左括號必須用相同型別的右括號閉合。

class Solution(object):
    def push(self,list,obj):
        list.append(obj)
        print('push ',obj,'successed and the index is ',len(list))
        return obj

    def pop(self,list):
        if(len(list)!=0):
            obj=list[-1]
            list.pop()
            print('pop',obj,'successed')
            return obj
        else:
            return None

    def getTop(self,list):
        if len(list)!=0:
            return list[-1]
        else:
            return None

    def isValid(self,s):
        sa=list(s)
        listA=[]
        listB=[]
        for _ in range(len(sa)):
            self.push(listA,sa[len(sa)-_-1])
        print('listA start : ',listA)

        for firstPop in range(len(sa)):
            if len(listA)==0:
                print('break')
                break
            topA=self.getTop(listA)
            if(topA=='('or topA=='[' or topA=='{'):
                self.pop(listA)
                self.push(listB,topA)

            else:
                b = self.getTop(listB)
                if((topA == ')' and b == '(') or (topA == ']' and b == '[') or (topA == '}' and b == '{')):
                    self.pop(listA)
                    self.pop(listB)
                else:
                    return False



        print('listA:', listA)
        print('listB:', listB)
        a=self.getTop(listA)
        b=self.getTop(listB)

        if(a==None and b==None):
            return True
        else:
            return False

未完待續