Skip to content

Commit ff2fad0

Browse files
committed
feat: update python solutions to lc problems
1 parent e86cfbc commit ff2fad0

File tree

84 files changed

+478
-331
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+478
-331
lines changed

lcci/17.20.Continuous Median/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ class MedianFinder:
5656
self.max_heap = []
5757

5858
def addNum(self, num: int) -> None:
59-
heapq.heappush(self.min_heap, num)
60-
heapq.heappush(self.max_heap, -heapq.heappop(self.min_heap))
59+
heappush(self.min_heap, num)
60+
heappush(self.max_heap, -heappop(self.min_heap))
6161
if len(self.max_heap) - len(self.min_heap) > 1:
62-
heapq.heappush(self.min_heap, -heapq.heappop(self.max_heap))
62+
heappush(self.min_heap, -heappop(self.max_heap))
6363

6464
def findMedian(self) -> float:
6565
if len(self.max_heap) > len(self.min_heap):

lcci/17.20.Continuous Median/README_EN.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ class MedianFinder:
5454
self.max_heap = []
5555

5656
def addNum(self, num: int) -> None:
57-
heapq.heappush(self.min_heap, num)
58-
heapq.heappush(self.max_heap, -heapq.heappop(self.min_heap))
57+
heappush(self.min_heap, num)
58+
heappush(self.max_heap, -heappop(self.min_heap))
5959
if len(self.max_heap) - len(self.min_heap) > 1:
60-
heapq.heappush(self.min_heap, -heapq.heappop(self.max_heap))
60+
heappush(self.min_heap, -heappop(self.max_heap))
6161

6262
def findMedian(self) -> float:
6363
if len(self.max_heap) > len(self.min_heap):

lcci/17.20.Continuous Median/Solution.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ def __init__(self):
77
self.max_heap = []
88

99
def addNum(self, num: int) -> None:
10-
heapq.heappush(self.min_heap, num)
11-
heapq.heappush(self.max_heap, -heapq.heappop(self.min_heap))
10+
heappush(self.min_heap, num)
11+
heappush(self.max_heap, -heappop(self.min_heap))
1212
if len(self.max_heap) - len(self.min_heap) > 1:
13-
heapq.heappush(self.min_heap, -heapq.heappop(self.max_heap))
13+
heappush(self.min_heap, -heappop(self.max_heap))
1414

1515
def findMedian(self) -> float:
1616
if len(self.max_heap) > len(self.min_heap):

lcof/面试题40. 最小的k个数/README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,14 @@
3333
### **Python3**
3434

3535
```python
36-
import heapq
37-
3836
class Solution:
3937
def getLeastNumbers(self, arr: List[int], k: int) -> List[int]:
4038
if k == 0:
4139
return []
4240
heap = []
4341
for e in arr:
44-
heapq.heappush(heap, e)
45-
return heapq.nsmallest(k, heap)
42+
heappush(heap, e)
43+
return nsmallest(k, heap)
4644
```
4745

4846
### **Java**
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
import heapq
2-
3-
41
class Solution:
52
def getLeastNumbers(self, arr: List[int], k: int) -> List[int]:
63
if k == 0:
74
return []
85
heap = []
96
for e in arr:
10-
heapq.heappush(heap, e)
11-
return heapq.nsmallest(k, heap)
7+
heappush(heap, e)
8+
return nsmallest(k, heap)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ class MedianFinder:
7171

7272
def addNum(self, num: int) -> None:
7373
if len(self.max_heap) == len(self.min_heap):
74-
heapq.heappush(self.min_heap, -heapq.heappushpop(self.max_heap, -num))
74+
heappush(self.min_heap, -heappushpop(self.max_heap, -num))
7575
else:
76-
heapq.heappush(self.max_heap, -heapq.heappushpop(self.min_heap, num))
76+
heappush(self.max_heap, -heappushpop(self.min_heap, num))
7777

7878
def findMedian(self) -> float:
7979
return (-self.max_heap[0] + self.min_heap[0]) / 2 if len(self.max_heap) == len(self.min_heap) else self.min_heap[0]

lcof/面试题41. 数据流中的中位数/Solution.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ def __init__(self):
88

99
def addNum(self, num: int) -> None:
1010
if len(self.max_heap) == len(self.min_heap):
11-
heapq.heappush(self.min_heap, -heapq.heappushpop(self.max_heap, -num))
11+
heappush(self.min_heap, -heappushpop(self.max_heap, -num))
1212
else:
13-
heapq.heappush(self.max_heap, -heapq.heappushpop(self.min_heap, num))
13+
heappush(self.max_heap, -heappushpop(self.min_heap, num))
1414

1515
def findMedian(self) -> float:
1616
return (

lcof/面试题59 - II. 队列的最大值/README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,7 @@
4646
<!-- 这里可写当前语言的特殊实现逻辑 -->
4747

4848
```python
49-
from collections import deque
50-
51-
5249
class MaxQueue:
53-
5450
def __init__(self):
5551
self.p = deque()
5652
self.q = deque()

lcof/面试题59 - II. 队列的最大值/Solution.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
from collections import deque
2-
3-
41
class MaxQueue:
52
def __init__(self):
63
self.p = deque()

lcof2/剑指 Offer II 059. 数据流的第 K 大数值/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ class KthLargest:
7272
self.add(num)
7373

7474
def add(self, val: int) -> int:
75-
heapq.heappush(self.q, val)
75+
heappush(self.q, val)
7676
if len(self.q) > self.size:
77-
heapq.heappop(self.q)
77+
heappop(self.q)
7878
return self.q[0]
7979

8080

0 commit comments

Comments
 (0)