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

Commitfa61f11

Browse files
committed
Update 003
Update 003
1 parent28ddde6 commitfa61f11

4 files changed

+106
-11
lines changed

‎Python3/001_Two_Sum.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!usr/bin/env python
2+
# -*- coding:utf-8 -*-
13
'''
24
Given an array of integers, find two numbers such that they add up to a specific target number.
35

‎Python3/002_Add_Two_Numbers.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!usr/bin/env python
2+
# -*- coding:utf-8 -*-
13
'''
24
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.
35
@@ -11,11 +13,10 @@ def __init__(self, x):
1113
self.val=x
1214
self.next=None
1315

14-
# Define this to check if it works well
15-
defmyPrint(self):
16+
defmy_test(self):
1617
print(self.val)
1718
ifself.next:
18-
self.next.myPrint()
19+
self.next.my_test()
1920

2021

2122
classSolution(object):
@@ -25,15 +26,14 @@ def addTwoNumbers(self, l1, l2):
2526
:type l2: ListNode
2627
:rtype: ListNode
2728
"""
28-
result=ListNode(0);
29-
cur=result;
29+
result=ListNode(0)
30+
cur=result
3031
whilel1orl2:
31-
cur.val+=self.addTwoNodes(l1,l2)
32+
cur.val+=self.AddTwoNodes(l1,l2)
3233
ifcur.val>=10:
3334
cur.val-=10
3435
cur.next=ListNode(1)
3536
else:
36-
# Check if there is need to make the next node
3737
ifl1andl1.nextorl2andl2.next:
3838
cur.next=ListNode(0)
3939
cur=cur.next
@@ -43,9 +43,9 @@ def addTwoNumbers(self, l1, l2):
4343
l2=l2.next
4444
returnresult
4545

46-
defaddTwoNodes(self,n1,n2):
46+
47+
defAddTwoNodes(self,n1,n2):
4748
ifnotn1andnotn2:
48-
# This cannot happen, ignore it
4949
None
5050
ifnotn1:
5151
returnn2.val
@@ -54,12 +54,16 @@ def addTwoNodes(self, n1, n2):
5454
returnn1.val+n2.val
5555

5656

57+
58+
59+
60+
5761
if__name__=="__main__":
5862
list=ListNode(2)
5963
list.next=ListNode(4)
6064
list.next.next=ListNode(3)
61-
list1=ListNode(5)
65+
list1=ListNode(5)
6266
list1.next=ListNode(6)
6367
list1.next.next=ListNode(4)
6468

65-
print(Solution().addTwoNumbers(list,list1).myPrint())
69+
print(Solution().addTwoNumbers(list,list1).my_test())
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!usr/bin/env python
2+
# -*- coding:utf-8 -*-
3+
'''
4+
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.
5+
'''
6+
7+
8+
classSolution(object):
9+
deflengthOfLongestSubstring(self,s):
10+
"""
11+
:type s: str
12+
:rtype: int
13+
"""
14+
ifnots:
15+
return0
16+
iflen(s)<=1:
17+
returnlen(s)
18+
locations= [-1foriinrange(256)]
19+
print(locations)
20+
index=-1
21+
m=0
22+
fori,vinenumerate(s):
23+
#如果出现的字符就需要更新index为当前字符的位置
24+
if (locations[ord(v)]>index):
25+
index=locations[ord(v)]
26+
m=max(m,i-index)
27+
#确定字符的位置,相同字符的位置一样
28+
locations[ord(v)]=i
29+
returnm
30+
31+
32+
if__name__=="__main__":
33+
print(Solution().lengthOfLongestSubstring("bccab"))
34+
35+
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!usr/bin/env python
2+
# -*- coding:utf-8 -*-
3+
4+
5+
'''
6+
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
7+
'''
8+
9+
classSolution(object):
10+
deffindMedianSortedArrays(self,nums1,nums2):
11+
"""
12+
:type nums1: List[int]
13+
:type nums2: List[int]
14+
:rtype: float
15+
"""
16+
length1=len(nums1)
17+
length2=len(nums2)
18+
k= (length1+length2)//2
19+
if (length1+length2)%2==0:
20+
return (self.findK(nums1,nums2,k)+self.findK(nums1,nums2,k-1))/2.0;# 2 is enough in python3
21+
else:
22+
returnself.findK(nums1,nums2,k)
23+
24+
deffindK(self,num1,num2,k):
25+
# Recursive ends here
26+
ifnotnum1:
27+
returnnum2[k]
28+
ifnotnum2:
29+
returnnum1[k]
30+
ifk==0:
31+
returnmin(num1[0],num2[0])
32+
33+
length1=len(num1)
34+
length2=len(num2)
35+
ifnum1[length1//2]>num2[length2//2]:
36+
ifk>length1//2+length2//2:
37+
returnself.findK(num1,num2[length2//2+1:],k-length2//2-1)
38+
else:
39+
returnself.findK(num1[:length1//2],num2,k)
40+
else:
41+
ifk>length1//2+length2//2:
42+
returnself.findK(num1[length1//2+1:],num2,k-length1//2-1)
43+
else:
44+
returnself.findK(num1,num2[:length2//2],k)
45+
46+
47+
if__name__=="__main__":
48+
assertSolution().findMedianSortedArrays([1,2], [1,2,3])==2
49+
assertSolution().findMedianSortedArrays([], [2,3])==2.5
50+
51+
52+
53+
54+

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp