Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c9acf48

Browse files
committed
add 1 & 2
1 parent d2b558d commit c9acf48

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ wheels/
2424
*.egg-info/
2525
.installed.cfg
2626
*.egg
27+
.DS_Store
2728

2829
# PyInstaller
2930
# Usually these files are written by a python script from a template

‎001/two_sum.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def twoSum(self, nums, target):
3+
"""
4+
:type nums: List[int]
5+
:type target: int
6+
:rtype: List[int]
7+
"""
8+
d = {}
9+
for i, num in enumerate(nums):
10+
if target - num in d:
11+
return [d[target - num], i]
12+
d[num] = i

‎002/add_two_numbers.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Definition for singly-linked list.
2+
class ListNode(object):
3+
def __init__(self, x):
4+
self.val = x
5+
self.next = None
6+
7+
8+
class Solution(object):
9+
def addTwoNumbers(self, l1, l2):
10+
"""
11+
:type l1: ListNode
12+
:type l2: ListNode
13+
:rtype: ListNode
14+
"""
15+
temp = 1 if (l1.val + l2.val) >= 10 else 0
16+
head = ListNode((l1.val + l2.val) % 10)
17+
tail = head
18+
while l1.next is not None or l2.next is not None or temp == 1:
19+
l1 = l1.next if l1.next is not None else ListNode(0)
20+
l2 = l2.next if l2.next is not None else ListNode(0)
21+
node = ListNode((l1.val + l2.val + temp) % 10)
22+
temp = 1 if (l1.val + l2.val + temp) >= 10 else 0
23+
tail.next = node
24+
tail = node
25+
return head

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /