Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LeetCode Answers for 01st and 2nd #189

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Python/0001. Two sum.py
Original file line number Diff line number Diff line change
@@ -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)
29 changes: 29 additions & 0 deletions Python/0002. Add two numbers.py
Original file line number Diff line number Diff line change
@@ -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