close
two-sum
class Solution: def twoSum(self, nums: 'List[int]', target: 'int') -> 'List[int]': tmp={} for i in range(len(nums)): if target-nums[i] in tmp: return(tmp[target-nums[i]],i) else: tmp[nums[i]]=i;add-two-numbers
# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def addTwoNumbers(self, l1: 'ListNode', l2: 'ListNode') -> 'ListNode': head=ListNode(0) answer=head carry=0 while l1 and l2: add=l1.val+l2.val+carry carry=1if add >=10 else 0 head.next=ListNode(add%10) head=head.next l1,l2=l1.next,l2.next l=l1 if l1 else l2 while l: add=l.val + carry carry=1 if add>=10 else 0
全站熱搜
留言列表