Skip to content

Commit 2b833f2

Browse files
committed
2019-05-19
1 parent b89a19c commit 2b833f2

File tree

6 files changed

+215
-0
lines changed

6 files changed

+215
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<p>There are two sorted arrays <b>nums1</b> and <b>nums2</b> of size m and n respectively.</p>
2+
3+
<p>Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).</p>
4+
5+
<p>You may assume <strong>nums1</strong> and <strong>nums2</strong>&nbsp;cannot be both empty.</p>
6+
7+
<p><b>Example 1:</b></p>
8+
9+
<pre>
10+
nums1 = [1, 3]
11+
nums2 = [2]
12+
13+
The median is 2.0
14+
</pre>
15+
16+
<p><b>Example 2:</b></p>
17+
18+
<pre>
19+
nums1 = [1, 2]
20+
nums2 = [3, 4]
21+
22+
The median is (2 + 3)/2 = 2.5
23+
</pre>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from heapq import *
2+
class MedianFinder(object):
3+
# 维护两个堆,一个大顶堆,一个小顶堆,小顶堆里的数比大顶堆里的数都要大,
4+
# 如果有两个潜在的中位数(两个堆size相同),数据流的中位数就是两个堆顶之和除以2
5+
# 如果只有一个中位数,就看size更小的那个堆的堆顶
6+
# 如果新进来的数比小顶堆的数要小,就把它插入大顶堆
7+
# 如果新进来的数比小顶堆的数要大,就把它插入小顶堆
8+
# 调整两个堆,使得size 差最大为1
9+
def __init__(self):
10+
"""
11+
initialize your data structure here.
12+
"""
13+
self.max_h = list()
14+
self.min_h = list()
15+
heapify(self.max_h)
16+
heapify(self.min_h)
17+
18+
19+
def addNum(self, num):
20+
"""
21+
:type num: int
22+
:rtype: None
23+
"""
24+
heappush(self.min_h, num)
25+
heappush(self.max_h, -heappop(self.min_h))
26+
if len(self.max_h) > len(self.min_h):
27+
heappush(self.min_h, -heappop(self.max_h))
28+
29+
def findMedian(self):
30+
"""
31+
:rtype: float
32+
"""
33+
max_len = len(self.max_h)
34+
min_len = len(self.min_h)
35+
if max_len == min_len: #有两个候选中位数
36+
return (self.min_h[0] + -self.max_h[0]) / 2.
37+
else:#小顶堆的size 一定 >= 大顶堆的size,所以答案就是小顶堆的堆顶
38+
return self.min_h[0] / 1.
39+
40+
# Your MedianFinder object will be instantiated and called as such:
41+
# obj = MedianFinder()
42+
# obj.addNum(num)
43+
# param_2 = obj.findMedian()
44+
45+
class Solution(object):
46+
def findMedianSortedArrays(self, nums1, nums2):
47+
"""
48+
:type nums1: List[int]
49+
:type nums2: List[int]
50+
:rtype: float
51+
"""
52+
mf = MedianFinder()
53+
for num in nums1:
54+
mf.addNum(num)
55+
for num in nums2:
56+
mf.addNum(num)
57+
return mf.findMedian()
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<p>Given two integers <code>dividend</code> and <code>divisor</code>, divide two integers without using multiplication, division and mod operator.</p>
2+
3+
<p>Return the quotient after dividing <code>dividend</code> by <code>divisor</code>.</p>
4+
5+
<p>The integer division should truncate toward zero.</p>
6+
7+
<p><strong>Example 1:</strong></p>
8+
9+
<pre>
10+
<strong>Input:</strong> dividend = 10, divisor = 3
11+
<strong>Output:</strong> 3</pre>
12+
13+
<p><strong>Example 2:</strong></p>
14+
15+
<pre>
16+
<strong>Input:</strong> dividend = 7, divisor = -3
17+
<strong>Output:</strong> -2</pre>
18+
19+
<p><strong>Note:</strong></p>
20+
21+
<ul>
22+
<li>Both dividend and divisor&nbsp;will be&nbsp;32-bit&nbsp;signed integers.</li>
23+
<li>The divisor will never be 0.</li>
24+
<li>Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [&minus;2<sup>31</sup>, &nbsp;2<sup>31</sup> &minus; 1]. For the purpose of this problem, assume that your function returns 2<sup>31</sup> &minus; 1 when the division result&nbsp;overflows.</li>
25+
</ul>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution(object):
2+
def divide(self, dividend, divisor):
3+
"""
4+
:type dividend: int
5+
:type divisor: int
6+
:rtype: int
7+
"""
8+
# 计算被除数可以减去多少个除数:
9+
op = 1
10+
if (dividend > 0 and divisor < 0) or (dividend < 0 and divisor > 0):
11+
op = -1
12+
13+
dividend, divisor = abs(dividend), abs(divisor)
14+
res = 0
15+
# cnt = 1
16+
while(dividend >= divisor):
17+
multidivisor, multi = divisor, 1
18+
while(dividend >= multidivisor):
19+
res += multi
20+
dividend -= multidivisor
21+
multi = multi << 1
22+
multidivisor = multidivisor <<1
23+
print dividend, multidivisor, multi
24+
print multi
25+
26+
27+
INT_MIN = -(2 **31)
28+
INT_MAX = 2 **31 - 1
29+
res *= op
30+
31+
return res if INT_MIN <= res <= INT_MAX else INT_MAX
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<p>Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value.</p>
2+
For example,
3+
4+
<p><code>[2,3,4]</code>, the median is <code>3</code></p>
5+
6+
<p><code>[2,3]</code>, the median is <code>(2 + 3) / 2 = 2.5</code></p>
7+
8+
<p>Design a data structure that supports the following two operations:</p>
9+
10+
<ul>
11+
<li>void addNum(int num) - Add a integer number from the data stream to the data structure.</li>
12+
<li>double findMedian() - Return the median of all elements so far.</li>
13+
</ul>
14+
15+
<p>&nbsp;</p>
16+
17+
<p><strong>Example:</strong></p>
18+
19+
<pre>
20+
addNum(1)
21+
addNum(2)
22+
findMedian() -&gt; 1.5
23+
addNum(3)
24+
findMedian() -&gt; 2
25+
</pre>
26+
27+
<p>&nbsp;</p>
28+
29+
<p><strong>Follow up:</strong></p>
30+
31+
<ol>
32+
<li>If all integer numbers from the stream are between 0&nbsp;and 100, how would you optimize it?</li>
33+
<li>If 99% of all integer numbers from the stream are between 0 and 100, how would you optimize it?</li>
34+
</ol>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from heapq import *
2+
class MedianFinder(object):
3+
# 维护两个堆,一个大顶堆,一个小顶堆,小顶堆里的数比大顶堆里的数都要大,
4+
# 如果有两个潜在的中位数(两个堆size相同),数据流的中位数就是两个堆顶之和除以2
5+
# 如果只有一个中位数,就看size更小的那个堆的堆顶
6+
# 新进来的数都丢进小顶堆,然后把小顶堆的堆顶丢到大顶堆,
7+
# 调整两个堆,使得size 差最大为1
8+
def __init__(self):
9+
"""
10+
initialize your data structure here.
11+
"""
12+
self.max_h = list()
13+
self.min_h = list()
14+
heapify(self.max_h)
15+
heapify(self.min_h)
16+
17+
18+
def addNum(self, num):
19+
"""
20+
:type num: int
21+
:rtype: None
22+
"""
23+
heappush(self.min_h, num)
24+
heappush(self.max_h, -heappop(self.min_h))
25+
if len(self.max_h) > len(self.min_h):
26+
heappush(self.min_h, -heappop(self.max_h))
27+
28+
def findMedian(self):
29+
"""
30+
:rtype: float
31+
"""
32+
max_len = len(self.max_h)
33+
min_len = len(self.min_h)
34+
if max_len == min_len: #有两个候选中位数
35+
return (self.min_h[0] + -self.max_h[0]) / 2.
36+
else:#小顶堆的size 一定 >= 大顶堆的size,所以答案就是小顶堆的堆顶
37+
return self.min_h[0] / 1.
38+
39+
40+
41+
42+
# Your MedianFinder object will be instantiated and called as such:
43+
# obj = MedianFinder()
44+
# obj.addNum(num)
45+
# param_2 = obj.findMedian()

0 commit comments

Comments
 (0)