Skip to content

Commit fa61f11

Browse files
committed
Update 003
Update 003
1 parent 28ddde6 commit fa61f11

4 files changed

+106
-11
lines changed

Python3/001_Two_Sum.py

+2
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

+15-11
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-
def myPrint(self):
16+
def my_test(self):
1617
print(self.val)
1718
if self.next:
18-
self.next.myPrint()
19+
self.next.my_test()
1920

2021

2122
class Solution(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
while l1 or l2:
31-
cur.val += self.addTwoNodes(l1, l2)
32+
cur.val += self.AddTwoNodes(l1, l2)
3233
if cur.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
if l1 and l1.next or l2 and l2.next:
3838
cur.next = ListNode(0)
3939
cur = cur.next
@@ -43,9 +43,9 @@ def addTwoNumbers(self, l1, l2):
4343
l2 = l2.next
4444
return result
4545

46-
def addTwoNodes(self, n1, n2):
46+
47+
def AddTwoNodes(self, n1, n2):
4748
if not n1 and not n2:
48-
# This cannot happen, ignore it
4949
None
5050
if not n1:
5151
return n2.val
@@ -54,12 +54,16 @@ def addTwoNodes(self, n1, n2):
5454
return n1.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())
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+
class Solution(object):
9+
def lengthOfLongestSubstring(self, s):
10+
"""
11+
:type s: str
12+
:rtype: int
13+
"""
14+
if not s:
15+
return 0
16+
if len(s) <= 1:
17+
return len(s)
18+
locations = [-1 for i in range(256)]
19+
print(locations)
20+
index = -1
21+
m = 0
22+
for i, v in enumerate(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+
return m
30+
31+
32+
if __name__ == "__main__":
33+
print(Solution().lengthOfLongestSubstring("bccab"))
34+
35+
+54
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+
class Solution(object):
10+
def findMedianSortedArrays(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+
return self.findK(nums1, nums2, k)
23+
24+
def findK(self, num1, num2, k):
25+
# Recursive ends here
26+
if not num1:
27+
return num2[k]
28+
if not num2:
29+
return num1[k]
30+
if k == 0:
31+
return min(num1[0], num2[0])
32+
33+
length1 = len(num1)
34+
length2 = len(num2)
35+
if num1[length1 // 2] > num2[length2 // 2]:
36+
if k > length1 // 2 + length2 // 2:
37+
return self.findK(num1, num2[length2 // 2 + 1:], k - length2 // 2 - 1)
38+
else:
39+
return self.findK(num1[:length1 // 2], num2, k)
40+
else:
41+
if k > length1 // 2 + length2 // 2:
42+
return self.findK(num1[length1 // 2 + 1:], num2, k - length1 // 2 - 1)
43+
else:
44+
return self.findK(num1, num2[:length2 // 2], k)
45+
46+
47+
if __name__ == "__main__":
48+
assert Solution().findMedianSortedArrays([1, 2], [1, 2, 3]) == 2
49+
assert Solution().findMedianSortedArrays([], [2, 3]) == 2.5
50+
51+
52+
53+
54+

0 commit comments

Comments
 (0)