Movatterモバイル変換


[0]ホーム

URL:


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

Commit8aa50a3

Browse files
committed
Merge branch '1-10'
2 parents6485e83 +a2ed854 commit8aa50a3

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed

‎.gitignore

Lines 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

Whitespace-only changes.

‎Python3/001_Two_Sum.py

Lines 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+
classSolution(object):
14+
deftwoSum(self,nums,target):
15+
"""
16+
:type nums: List[int]
17+
:type target: int
18+
:rtype: List[int]
19+
"""
20+
dist= {}
21+
forindex,valueinenumerate(nums):
22+
dist[value]=index
23+
forindex1,valueinenumerate(nums):
24+
iftarget-valueindist:
25+
index2=dist[target-value]
26+
ifindex1!=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

Lines 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+
classListNode(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+
defmyPrint(self):
16+
print(self.val)
17+
ifself.next:
18+
self.next.myPrint()
19+
20+
21+
classSolution(object):
22+
defaddTwoNumbers(self,l1,l2):
23+
"""
24+
:type l1: ListNode
25+
:type l2: ListNode
26+
:rtype: ListNode
27+
"""
28+
result=ListNode(0);
29+
cur=result;
30+
whilel1orl2:
31+
cur.val+=self.addTwoNodes(l1,l2)
32+
ifcur.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+
ifl1andl1.nextorl2andl2.next:
38+
cur.next=ListNode(0)
39+
cur=cur.next
40+
ifl1:
41+
l1=l1.next
42+
ifl2:
43+
l2=l2.next
44+
returnresult
45+
46+
defaddTwoNodes(self,n1,n2):
47+
ifnotn1andnotn2:
48+
# This cannot happen, ignore it
49+
None
50+
ifnotn1:
51+
returnn2.val
52+
ifnotn2:
53+
returnn1.val
54+
returnn1.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)

[8]ページ先頭

©2009-2025 Movatter.jp