From 6a3cfa9ec3534a521c0fc88ea930ad1683b31263 Mon Sep 17 00:00:00 2001 From: 1 Date: Sat, 23 Dec 2023 11:34:00 +0530 Subject: [PATCH] added 01/02 --- Python/0001. Two sum.py | 13 +++++++++++++ Python/0002. Add two numbers.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 Python/0001. Two sum.py create mode 100644 Python/0002. Add two numbers.py diff --git a/Python/0001. Two sum.py b/Python/0001. Two sum.py new file mode 100644 index 0000000000..57939d9097 --- /dev/null +++ b/Python/0001. Two sum.py @@ -0,0 +1,13 @@ +class Solution(object): + def twoSum(self, nums, target): + for i in range(len(nums)): + for j in range(i+1,len(nums)): + if nums[i]+nums[j]==target: + return [i,j] + + +if __name__ == "__main__": + nums = [2,7,11,15] + target = 9 + result = Solution().twoSum(nums,target) + print(result) \ No newline at end of file diff --git a/Python/0002. Add two numbers.py b/Python/0002. Add two numbers.py new file mode 100644 index 0000000000..74c997aadb --- /dev/null +++ b/Python/0002. Add two numbers.py @@ -0,0 +1,29 @@ +class ListNode(object): + def __init__(self, val=0, next=None): + self.val = val + self.next = next + +class Solution(object): + def addTwoNumbers(self,l1: ListNode,l2: ListNode) -> ListNode: + dummy = ListNode() + current = dummy + + carry = 0 + while l1 or l2 or carry: + v1 = l1.val if l1 else 0 + v2 = l2.val if l2 else 0 + + # new digit + val = v1 + v2 + carry + carry = val//10 + val = val%10 + current.next = ListNode(val) + + current = current.next + l1 = l1.next if l1 else None + l2 = l2.next if l2 else None + + return dummy.next + +if __name__ == "__main__": + pass \ No newline at end of file