Problem 1 and 2
This commit is contained in:
9
0001-two-sum/solution.py
Normal file
9
0001-two-sum/solution.py
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
class Solution:
|
||||||
|
def twoSum(self, nums: List[int], target: int) -> List[int]:
|
||||||
|
dic = {}
|
||||||
|
for i, val in enumerate(nums):
|
||||||
|
if target - val in dic:
|
||||||
|
return [dic[target - val], i]
|
||||||
|
else:
|
||||||
|
dic[val] = i
|
||||||
|
|
||||||
22
0002-add-two-numbers/solution.py
Normal file
22
0002-add-two-numbers/solution.py
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
# Definition for singly-linked list.
|
||||||
|
# class ListNode:
|
||||||
|
# def __init__(self, val=0, next=None):
|
||||||
|
# self.val = val
|
||||||
|
# self.next = next
|
||||||
|
class Solution:
|
||||||
|
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
|
||||||
|
i = 1
|
||||||
|
n1 = n2 = 0
|
||||||
|
while not (l1 == None and l2 == None):
|
||||||
|
if l1 != None:
|
||||||
|
n1 += (l1.val * i)
|
||||||
|
l1 = l1.next
|
||||||
|
if l2 != None:
|
||||||
|
n2 += (l2.val * i)
|
||||||
|
l2 = l2.next
|
||||||
|
i *= 10
|
||||||
|
n = None
|
||||||
|
for c in list(str(n1 + n2)):
|
||||||
|
n = ListNode(int(c), n)
|
||||||
|
return n
|
||||||
|
|
||||||
Reference in New Issue
Block a user