Skip to content

Commit 2979e72

Browse files
author
caiminchao
committed
add leetcode 1 and 2
1 parent ad042a5 commit 2979e72

File tree

1 file changed

+61
-1
lines changed

1 file changed

+61
-1
lines changed

solution.py

+61-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,61 @@
1-
'FIRST LINE'
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
"""
5+
@version: v1.0
6+
@author: caiminchao
7+
8+
@software: PyCharm,python 2.7
9+
@file: test
10+
@time: 2019/5/23 20:51
11+
"""
12+
class ListNode:
13+
def __init__(self, x):
14+
self.val = x
15+
self.next = None
16+
17+
class Solution(object):
18+
def twoSum(self, nums, target):
19+
"""
20+
:type nums: List[int]
21+
:type target: int
22+
:rtype: List[int]
23+
"""
24+
res = {}
25+
for i in xrange(len(nums)):
26+
if target-nums[i] in res:
27+
return [res[target-nums[i]],i]
28+
res[nums[i]] = i
29+
30+
def addTwoNumbers(self, l1, l2):
31+
"""
32+
:type l1: ListNode
33+
:type l2: ListNode
34+
:rtype: ListNode
35+
"""
36+
carry = 0
37+
n = ListNode(0)
38+
root = n
39+
while l1 or l2 or carry:
40+
v1=0
41+
v2=0
42+
if l1:
43+
v1 = l1.val
44+
l1 = l1.next
45+
if l2:
46+
v2 = l2.val
47+
l2 = l2.next
48+
carry ,val = divmod(v1+v2+carry,10)
49+
n.next = ListNode(val)
50+
n=n.next
51+
return root.next
52+
53+
54+
if __name__=='__main__':
55+
l1 = ListNode(2)
56+
l1.next = ListNode(4)
57+
l1.next.next = ListNode(3)
58+
l2=ListNode(5)
59+
l2.next = ListNode(6)
60+
l2.next.next = ListNode(4)
61+
print Solution().addTwoNumbers(l1,l2).val

0 commit comments

Comments
 (0)