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

Appearance settings

Commit a2ed854

Browse filesBrowse files
committed
Update
Update
1 parent 6485e83 commit a2ed854
Copy full SHA for a2ed854

File tree

Expand file treeCollapse file tree

4 files changed

+96
-0
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+96
-0
lines changed

‎.gitignore

Copy file name to clipboardExpand all lines: .gitignore
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,5 @@ docs/_build/
5656

5757
# PyBuilder
5858
target/
59+
60+
\.idea/

‎Python3/001 Two Sum.py

Copy file name to clipboardExpand all lines: Python3/001 Two Sum.py
Whitespace-only changes.

‎Python3/001_Two_Sum.py

Copy file name to clipboard
+34Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'''
2+
Given an array of integers, find two numbers such that they add up to a specific target number.
3+
4+
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
5+
6+
You may assume that each input would have exactly one solution.
7+
8+
Input: numbers={2, 7, 11, 15}, target=9
9+
Output: index1=1, index2=2
10+
'''
11+
12+
13+
class Solution(object):
14+
def twoSum(self, nums, target):
15+
"""
16+
:type nums: List[int]
17+
:type target: int
18+
:rtype: List[int]
19+
"""
20+
dist = {}
21+
for index, value in enumerate(nums):
22+
dist[value] = index
23+
for index1, value in enumerate(nums):
24+
if target - value in dist:
25+
index2 = dist[target - value]
26+
if index1 != index2:
27+
return [index1+1, index2+1]
28+
29+
30+
if __name__ == '__main__':
31+
print(Solution().twoSum([2, 7, 11, 15], 9))
32+
33+
34+

‎Python3/002_Add_Two_Numbers.py

Copy file name to clipboard
+60Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
'''
2+
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
3+
4+
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
5+
Output: 7 -> 0 -> 8
6+
'''
7+
8+
# Definition for singly-linked list.
9+
class ListNode(object):
10+
def __init__(self, x):
11+
self.val = x
12+
self.next = None
13+
14+
# Define this to check if it works well
15+
def myPrint(self):
16+
print(self.val)
17+
if self.next:
18+
self.next.myPrint()
19+
20+
21+
class Solution(object):
22+
def addTwoNumbers(self, l1, l2):
23+
"""
24+
:type l1: ListNode
25+
:type l2: ListNode
26+
:rtype: ListNode
27+
"""
28+
result = ListNode(0);
29+
cur = result;
30+
while l1 or l2:
31+
cur.val += self.addTwoNodes(l1, l2)
32+
if cur.val >= 10:
33+
cur.val -= 10
34+
cur.next = ListNode(1)
35+
else:
36+
# Check if there is need to make the next node
37+
if l1 and l1.next or l2 and l2.next:
38+
cur.next = ListNode(0)
39+
cur = cur.next
40+
if l1:
41+
l1 = l1.next
42+
if l2:
43+
l2 = l2.next
44+
return result
45+
46+
def addTwoNodes(self, n1, n2):
47+
if not n1 and not n2:
48+
# This cannot happen, ignore it
49+
None
50+
if not n1:
51+
return n2.val
52+
if not n2:
53+
return n1.val
54+
return n1.val + n2.val
55+
56+
57+
if __name__ == "__main__":
58+
list = ListNode(9)
59+
list.next = ListNode(8)
60+
print(Solution().addTwoNumbers(list, ListNode(1)).myPrint())

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.