Skip to content

Commit 5a3dc9e

Browse files
committedApr 7, 2020
feat: add python and java solutions to lcof question
添加《剑指 Offer》题解:面试题41. 数据流中的中位数
1 parent bf0fc5e commit 5a3dc9e

File tree

6 files changed

+129
-8
lines changed

6 files changed

+129
-8
lines changed
 

‎lcof/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
| [45](https://leetcode-cn.com/problems/ba-shu-zu-pai-cheng-zui-xiao-de-shu-lcof) | [把数组排成最小的数](/lcof/%E9%9D%A2%E8%AF%95%E9%A2%9845.%20%E6%8A%8A%E6%95%B0%E7%BB%84%E6%8E%92%E6%88%90%E6%9C%80%E5%B0%8F%E7%9A%84%E6%95%B0/README.md) | `排序` | 中等 |
5656
| [46](https://leetcode-cn.com/problems/ba-shu-zi-fan-yi-cheng-zi-fu-chuan-lcof) | [把数字翻译成字符串](/lcof/%E9%9D%A2%E8%AF%95%E9%A2%9846.%20%E6%8A%8A%E6%95%B0%E5%AD%97%E7%BF%BB%E8%AF%91%E6%88%90%E5%AD%97%E7%AC%A6%E4%B8%B2/README.md) | | 中等 |
5757
| [47](https://leetcode-cn.com/problems/li-wu-de-zui-da-jie-zhi-lcof) | [礼物的最大价值](/lcof/%E9%9D%A2%E8%AF%95%E9%A2%9847.%20%E7%A4%BC%E7%89%A9%E7%9A%84%E6%9C%80%E5%A4%A7%E4%BB%B7%E5%80%BC/README.md) | `动态规划` | 中等 |
58-
| [48](https://leetcode-cn.com/problems/zui-chang-bu-han-zhong-fu-zi-fu-de-zi-zi-fu-chuan-lcof) | [最长不含重复字符的子字符串](/lcof/%E9%9D%A2%E8%AF%95%E9%A2%9848.%20%E6%9C%80%E9%95%BF%E4%B8%8D%E5%90%AB%E9%87%8D%E5%A4%8D%E5%AD%97%E7%AC%A6%E7%9A%84%E5%AD%90%E5%AD%97%E7%AC%A6%E4%B8%B2/README.md) | `哈希表`,`双指针`,`None` | 中等 |
58+
| [48](https://leetcode-cn.com/problems/zui-chang-bu-han-zhong-fu-zi-fu-de-zi-zi-fu-chuan-lcof) | [最长不含重复字符的子字符串](/lcof/%E9%9D%A2%E8%AF%95%E9%A2%9848.%20%E6%9C%80%E9%95%BF%E4%B8%8D%E5%90%AB%E9%87%8D%E5%A4%8D%E5%AD%97%E7%AC%A6%E7%9A%84%E5%AD%90%E5%AD%97%E7%AC%A6%E4%B8%B2/README.md) | `哈希表`,`双指针` | 中等 |
5959
| [49](https://leetcode-cn.com/problems/chou-shu-lcof) | [丑数](/lcof/%E9%9D%A2%E8%AF%95%E9%A2%9849.%20%E4%B8%91%E6%95%B0/README.md) | `数学` | 中等 |
6060
| [50](https://leetcode-cn.com/problems/di-yi-ge-zhi-chu-xian-yi-ci-de-zi-fu-lcof) | [第一个只出现一次的字符](/lcof/%E9%9D%A2%E8%AF%95%E9%A2%9850.%20%E7%AC%AC%E4%B8%80%E4%B8%AA%E5%8F%AA%E5%87%BA%E7%8E%B0%E4%B8%80%E6%AC%A1%E7%9A%84%E5%AD%97%E7%AC%A6/README.md) | `哈希表` | 简单 |
6161
| [51](https://leetcode-cn.com/problems/shu-zu-zhong-de-ni-xu-dui-lcof) | [数组中的逆序对](/lcof/%E9%9D%A2%E8%AF%95%E9%A2%9851.%20%E6%95%B0%E7%BB%84%E4%B8%AD%E7%9A%84%E9%80%86%E5%BA%8F%E5%AF%B9/README.md) | | 困难 |
@@ -71,8 +71,8 @@
7171
| [57 - II](https://leetcode-cn.com/problems/he-wei-sde-lian-xu-zheng-shu-xu-lie-lcof) | [和为s的连续正数序列](/lcof/%E9%9D%A2%E8%AF%95%E9%A2%9857%20-%20II.%20%E5%92%8C%E4%B8%BAs%E7%9A%84%E8%BF%9E%E7%BB%AD%E6%AD%A3%E6%95%B0%E5%BA%8F%E5%88%97/README.md) | | 简单 |
7272
| [58 - I](https://leetcode-cn.com/problems/fan-zhuan-dan-ci-shun-xu-lcof) | [翻转单词顺序](/lcof/%E9%9D%A2%E8%AF%95%E9%A2%9858%20-%20I.%20%E7%BF%BB%E8%BD%AC%E5%8D%95%E8%AF%8D%E9%A1%BA%E5%BA%8F/README.md) | `字符串` | 简单 |
7373
| [58 - II](https://leetcode-cn.com/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof) | [左旋转字符串](/lcof/%E9%9D%A2%E8%AF%95%E9%A2%9858%20-%20II.%20%E5%B7%A6%E6%97%8B%E8%BD%AC%E5%AD%97%E7%AC%A6%E4%B8%B2/README.md) | `字符串` | 简单 |
74-
| [59 - I](https://leetcode-cn.com/problems/hua-dong-chuang-kou-de-zui-da-zhi-lcof) | [滑动窗口的最大值](/lcof/%E9%9D%A2%E8%AF%95%E9%A2%9859%20-%20I.%20%E6%BB%91%E5%8A%A8%E7%AA%97%E5%8F%A3%E7%9A%84%E6%9C%80%E5%A4%A7%E5%80%BC/README.md) | ``,`None` | 简单 |
75-
| [59 - II](https://leetcode-cn.com/problems/dui-lie-de-zui-da-zhi-lcof) | [队列的最大值](/lcof/%E9%9D%A2%E8%AF%95%E9%A2%9859%20-%20II.%20%E9%98%9F%E5%88%97%E7%9A%84%E6%9C%80%E5%A4%A7%E5%80%BC/README.md) | ``,`None` | 中等 |
74+
| [59 - I](https://leetcode-cn.com/problems/hua-dong-chuang-kou-de-zui-da-zhi-lcof) | [滑动窗口的最大值](/lcof/%E9%9D%A2%E8%AF%95%E9%A2%9859%20-%20I.%20%E6%BB%91%E5%8A%A8%E7%AA%97%E5%8F%A3%E7%9A%84%E6%9C%80%E5%A4%A7%E5%80%BC/README.md) | `` | 简单 |
75+
| [59 - II](https://leetcode-cn.com/problems/dui-lie-de-zui-da-zhi-lcof) | [队列的最大值](/lcof/%E9%9D%A2%E8%AF%95%E9%A2%9859%20-%20II.%20%E9%98%9F%E5%88%97%E7%9A%84%E6%9C%80%E5%A4%A7%E5%80%BC/README.md) | `` | 中等 |
7676
| [60](https://leetcode-cn.com/problems/nge-tou-zi-de-dian-shu-lcof) | [n个骰子的点数](/lcof/%E9%9D%A2%E8%AF%95%E9%A2%9860.%20n%E4%B8%AA%E9%AA%B0%E5%AD%90%E7%9A%84%E7%82%B9%E6%95%B0/README.md) | | 简单 |
7777
| [61](https://leetcode-cn.com/problems/bu-ke-pai-zhong-de-shun-zi-lcof) | [扑克牌中的顺子](/lcof/%E9%9D%A2%E8%AF%95%E9%A2%9861.%20%E6%89%91%E5%85%8B%E7%89%8C%E4%B8%AD%E7%9A%84%E9%A1%BA%E5%AD%90/README.md) | | 简单 |
7878
| [62](https://leetcode-cn.com/problems/yuan-quan-zhong-zui-hou-sheng-xia-de-shu-zi-lcof) | [圆圈中最后剩下的数字](/lcof/%E9%9D%A2%E8%AF%95%E9%A2%9862.%20%E5%9C%86%E5%9C%88%E4%B8%AD%E6%9C%80%E5%90%8E%E5%89%A9%E4%B8%8B%E7%9A%84%E6%95%B0%E5%AD%97/README.md) | | 简单 |

‎lcof/面试题41. 数据流中的中位数/README.md

+63-2
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,81 @@
4141

4242
## 解法
4343
<!-- 这里可写通用的实现逻辑 -->
44-
44+
- 创建大根堆、小根堆,其中:大根堆存放较小的一半元素,小根堆存放较大的一半元素。
45+
- 添加元素时,若两堆元素个数相等,放入小根堆(使得小根堆个数多1);若不等,放入大根堆(使得大小根堆元素个数相等)
46+
- 取中位数时,若两队元素个数相等,取两堆顶求平均值;若不等,取小根堆堆顶。
4547

4648
### Python3
4749
<!-- 这里可写当前语言的特殊实现逻辑 -->
4850

4951
```python
52+
class MedianFinder:
53+
54+
def __init__(self):
55+
"""
56+
initialize your data structure here.
57+
"""
58+
self.max_heap = []
59+
self.min_heap = []
60+
61+
62+
def addNum(self, num: int) -> None:
63+
if len(self.max_heap) == len(self.min_heap):
64+
heapq.heappush(self.min_heap, -heapq.heappushpop(self.max_heap, -num))
65+
else:
66+
heapq.heappush(self.max_heap, -heapq.heappushpop(self.min_heap, num))
5067

68+
def findMedian(self) -> float:
69+
return (-self.max_heap[0] + self.min_heap[0]) / 2 if len(self.max_heap) == len(self.min_heap) else self.min_heap[0]
70+
71+
72+
# Your MedianFinder object will be instantiated and called as such:
73+
# obj = MedianFinder()
74+
# obj.addNum(num)
75+
# param_2 = obj.findMedian()
5176
```
5277

5378
### Java
5479
<!-- 这里可写当前语言的特殊实现逻辑 -->
5580

5681
```java
57-
82+
class MedianFinder {
83+
Queue<Integer> minHeap;
84+
Queue<Integer> maxHeap;
85+
86+
/** initialize your data structure here. */
87+
public MedianFinder() {
88+
minHeap = new PriorityQueue<>();
89+
maxHeap = new PriorityQueue<>((a, b) -> b - a);
90+
}
91+
92+
public void addNum(int num) {
93+
if (maxHeap.size() == minHeap.size()) {
94+
maxHeap.offer(num);
95+
// 放入小根堆(小根堆多1)
96+
minHeap.offer(maxHeap.poll());
97+
} else {
98+
minHeap.offer(num);
99+
// 放入大根堆(大小堆数量相等)
100+
maxHeap.offer(minHeap.poll());
101+
}
102+
}
103+
104+
public double findMedian() {
105+
if (((maxHeap.size() + minHeap.size()) & 1) == 0) {
106+
// 偶数个,取两个堆顶平均值
107+
return (maxHeap.peek() + minHeap.peek()) / 2.0;
108+
}
109+
return minHeap.peek();
110+
}
111+
}
112+
113+
/**
114+
* Your MedianFinder object will be instantiated and called as such:
115+
* MedianFinder obj = new MedianFinder();
116+
* obj.addNum(num);
117+
* double param_2 = obj.findMedian();
118+
*/
58119
```
59120

60121
### ...
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class MedianFinder {
2+
Queue<Integer> minHeap;
3+
Queue<Integer> maxHeap;
4+
5+
/** initialize your data structure here. */
6+
public MedianFinder() {
7+
minHeap = new PriorityQueue<>();
8+
maxHeap = new PriorityQueue<>((a, b) -> b - a);
9+
}
10+
11+
public void addNum(int num) {
12+
if (maxHeap.size() == minHeap.size()) {
13+
maxHeap.offer(num);
14+
// 放入小根堆(小根堆多1)
15+
minHeap.offer(maxHeap.poll());
16+
} else {
17+
minHeap.offer(num);
18+
// 放入大根堆(大小堆数量相等)
19+
maxHeap.offer(minHeap.poll());
20+
}
21+
}
22+
23+
public double findMedian() {
24+
if (((maxHeap.size() + minHeap.size()) & 1) == 0) {
25+
// 偶数个,取两个堆顶平均值
26+
return (maxHeap.peek() + minHeap.peek()) / 2.0;
27+
}
28+
return minHeap.peek();
29+
}
30+
}
31+
32+
/**
33+
* Your MedianFinder object will be instantiated and called as such:
34+
* MedianFinder obj = new MedianFinder();
35+
* obj.addNum(num);
36+
* double param_2 = obj.findMedian();
37+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class MedianFinder:
2+
3+
def __init__(self):
4+
"""
5+
initialize your data structure here.
6+
"""
7+
self.max_heap = []
8+
self.min_heap = []
9+
10+
11+
def addNum(self, num: int) -> None:
12+
if len(self.max_heap) == len(self.min_heap):
13+
heapq.heappush(self.min_heap, -heapq.heappushpop(self.max_heap, -num))
14+
else:
15+
heapq.heappush(self.max_heap, -heapq.heappushpop(self.min_heap, num))
16+
17+
def findMedian(self) -> float:
18+
return (-self.max_heap[0] + self.min_heap[0]) / 2 if len(self.max_heap) == len(self.min_heap) else self.min_heap[0]
19+
20+
21+
# Your MedianFinder object will be instantiated and called as such:
22+
# obj = MedianFinder()
23+
# obj.addNum(num)
24+
# param_2 = obj.findMedian()

‎lcof/面试题65. 不用加减乘除做加法/README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ class Solution:
3333
s = a ^ b
3434
carry = ((a & b) << 1) & 0xffffffff
3535
a, b = s, carry
36-
# 若a是正数,直接返回;若是负数,转为原码
37-
return a if a < 0x80000000 else ~(a^0xffffffff)
36+
return a if a < 0x80000000 else ~(a ^ 0xffffffff)
3837
```
3938

4039
### Java

‎lcof/面试题65. 不用加减乘除做加法/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ def add(self, a: int, b: int) -> int:
66
s = a ^ b
77
carry = ((a & b) << 1) & 0xffffffff
88
a, b = s, carry
9-
return a if a < 0x80000000 else ~(a^0xffffffff)
9+
return a if a < 0x80000000 else ~(a ^ 0xffffffff)

0 commit comments

Comments
 (0)