Skip to content

Commit 4846148

Browse files
author
robot
committed
feat: 0.8.19
1 parent c274c76 commit 4846148

File tree

4 files changed

+68
-147
lines changed

4 files changed

+68
-147
lines changed
Binary file not shown.

‎public/manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"manifest_version": 2,
33
"name": "leetcode cheatsheet",
44
"description": "刷题小助手,made by 力扣加加",
5-
"version": "0.8.18",
5+
"version": "0.8.19",
66
"browser_action": {
77
"default_popup": "index.html",
88
"default_title": "力扣加加"

‎src/codeTemplates/heap.js

+63-94
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,72 @@
11
const minHeapJSCode = `
2-
class MinHeap {
3-
4-
constructor () {
5-
/* Initialing the array heap and adding a dummy element at index 0 */
6-
this.heap = [null]
2+
function minHeap(A = []) {
3+
this.heapify(A);
4+
}
5+
6+
minHeap.prototype._shiftUp = function(i) {
7+
let parent_i = (i / 2) >>> 0;
8+
while (parent_i > 0) {
9+
if (this.h[i] < this.h[parent_i]) {
10+
[this.h[i], this.h[parent_i]] = [this.h[parent_i], this.h[i]];
11+
}
12+
parent_i = (parent_i / 2) >>> 0;
713
}
8-
9-
peek() {
10-
/* Accessing the min element at index 1 in the heap array */
11-
return this.heap[1]
14+
};
15+
16+
minHeap.prototype._shiftDown = function(i) {
17+
while (i * 2 <= this.h.length - 1) {
18+
const mc = this._minChild(i);
19+
if (this.h[i] > this.h[mc]) {
20+
[this.h[i], this.h[mc]] = [this.h[mc], this.h[i]];
21+
}
22+
i = mc;
1223
}
13-
14-
push (node) {
15-
16-
/* Inserting the new node at the end of the heap array */
17-
this.heap.push(node)
18-
19-
/* Finding the correct position for the new node */
20-
21-
if (this.heap.length > 1) {
22-
let current = this.heap.length - 1
23-
24-
/* Traversing up the parent node until the current node (current) is greater than the parent (current/2)*/
25-
while (current > 1 && this.heap[Math.floor(current/2)] > this.heap[current]) {
26-
27-
/* Swapping the two nodes by using the ES6 destructuring syntax*/
28-
[this.heap[Math.floor(current/2)], this.heap[current]] = [this.heap[current], this.heap[Math.floor(current/2)]]
29-
current = Math.floor(current/2)
30-
}
31-
}
32-
}
33-
34-
pop() {
35-
/* Smallest element is at the index 1 in the heap array */
36-
let smallest = this.heap[1]
37-
38-
/* When there are more than two elements in the array, we put the right most element at the first position
39-
and start comparing nodes with the child nodes
40-
*/
41-
if (this.heap.length > 2) {
42-
this.heap[1] = this.heap[this.heap.length-1]
43-
this.heap.splice(this.heap.length - 1)
44-
45-
if (this.heap.length === 3) {
46-
if (this.heap[1] > this.heap[2]) {
47-
[this.heap[1], this.heap[2]] = [this.heap[2], this.heap[1]]
48-
}
49-
return smallest
50-
}
51-
52-
let current = 1
53-
let leftChildIndex = current * 2
54-
let rightChildIndex = current * 2 + 1
55-
56-
while (this.heap[leftChildIndex] &&
57-
this.heap[rightChildIndex] &&
58-
(this.heap[current] > this.heap[leftChildIndex] ||
59-
this.heap[current] > this.heap[rightChildIndex])) {
60-
if (this.heap[leftChildIndex] < this.heap[rightChildIndex]) {
61-
[this.heap[current], this.heap[leftChildIndex]] = [this.heap[leftChildIndex], this.heap[current]]
62-
current = leftChildIndex
63-
} else {
64-
[this.heap[current], this.heap[rightChildIndex]] = [this.heap[rightChildIndex], this.heap[current]]
65-
current = rightChildIndex
66-
}
67-
68-
leftChildIndex = current * 2
69-
rightChildIndex = current * 2 + 1
70-
}
71-
}
72-
73-
/* If there are only two elements in the array, we directly splice out the first element */
74-
75-
else if (this.heap.length === 2) {
76-
this.heap.splice(1, 1)
77-
} else {
78-
return null
79-
}
80-
81-
return smallest
24+
};
25+
26+
minHeap.prototype._minChild = function(i) {
27+
if (i * 2 + 1 > this.h.length - 1) return i * 2;
28+
if (this.h[i * 2] < this.h[i * 2 + 1]) return i * 2;
29+
return i * 2 + 1;
30+
};
31+
32+
minHeap.prototype.pop = function() {
33+
if (this.h.length === 1) throw new Error('空了就别弹了吧?');
34+
const ans = this.h[1];
35+
this.h[1] = this.h[this.h.length - 1];
36+
this.h.pop();
37+
this._shiftDown(1);
38+
return ans;
39+
};
40+
41+
minHeap.prototype.push = function(a) {
42+
this.h.push(a);
43+
this._shiftUp(this.h.length - 1);
44+
console.log(this.h);
45+
};
46+
47+
minHeap.prototype.heapify = function(A) {
48+
this.h = [0].concat(A);
49+
i = 1;
50+
while (i < this.h.length) {
51+
this._shiftDown(i);
52+
i++;
8253
}
83-
/**
84-
* Your MinHeap object will be instantiated and called as such:
85-
* var obj = new MinHeap()
86-
* obj.push(1)
87-
* obj.push(2)
88-
* obj.peek() // will return 1
89-
* obj.pop() // remove 1
90-
* obj.peek() // will return 2
91-
*/
54+
};
55+
56+
// test:
57+
h = new minHeap([1, 2, 3]);
58+
h.push(4);
59+
h.push(5);
60+
h.pop();
61+
h.pop();
62+
console.log(h.h); // inspect internal value
9263
}
9364
`;
9465

9566
const minHeapPythonCode = `
9667
class min_heap:
97-
def __init__(self):
98-
self.h = [0]
68+
def __init__(self, A=[]):
69+
self.heapify(A)
9970
10071
def shift_up(self, i):
10172
while i // 2 > 0:
@@ -131,7 +102,7 @@ class min_heap:
131102
self.h.append(a)
132103
self.shift_up(len(self.h)-1)
133104
134-
def build_heap(self, A):
105+
def heapify(self, A):
135106
self.h = [0] + A
136107
i = 1
137108
while (i < len(self.h)):
@@ -140,8 +111,7 @@ class min_heap:
140111
141112
# 使用:
142113
143-
h = min_heap()
144-
h.build_heap([5, 6, 2, 3])
114+
h = min_heap([5, 6, 2, 3])
145115
146116
h.heappush(1)
147117
h.heappop() # 1
@@ -293,6 +263,5 @@ module.exports = {
293263
],
294264
},
295265
],
296-
link:
297-
"https://leetcode-solution.cn/solutionDetail?url=https%3A%2F%2Fapi.github.com%2Frepos%2Fazl397985856%2Fleetcode%2Fcontents%2Fthinkings%2Fheap.md&type=1",
266+
link: "https://leetcode-solution.cn/solutionDetail?url=https%3A%2F%2Fapi.github.com%2Frepos%2Fazl397985856%2Fleetcode%2Fcontents%2Fthinkings%2Fheap.md&type=1",
298267
};

‎src/db/root.db.js

+4-52
Original file line numberDiff line numberDiff line change
@@ -2525,60 +2525,12 @@
25252525
"sort-colors":{
25262526
"id": "75",
25272527
"name": "sort-colors",
2528-
"pre": [
2529-
{
2530-
"text": "荷兰国旗问题",
2531-
"link": "https://en.wikipedia.org/wiki/Dutch_national_flag_problem",
2532-
"color": "purple"
2533-
},
2534-
{
2535-
"text": "排序",
2536-
"link": null,
2537-
"color": "purple"
2538-
}
2539-
],
2540-
"keyPoints": [
2541-
{
2542-
"text": "荷兰国旗问题",
2543-
"link": null,
2544-
"color": "blue"
2545-
},
2546-
{
2547-
"text": "countingsort",
2548-
"link": null,
2549-
"color": "blue"
2550-
}
2551-
],
2552-
"companies": [
2553-
{
2554-
"name": "阿里巴巴"
2555-
},
2556-
{
2557-
"name": "腾讯"
2558-
},
2559-
{
2560-
"name": "百度"
2561-
},
2562-
{
2563-
"name": "字节跳动"
2564-
}
2565-
],
2528+
"pre": [],
2529+
"keyPoints": [],
2530+
"companies": [],
25662531
"giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/75.sort-colors.md",
25672532
"solution": "https://github.com/azl397985856/leetcode/blob/master/problems/75.sort-colors.md",
2568-
"code": [
2569-
{
2570-
"language": "cpp",
2571-
"text": "\nclass Solution {\npublic:\n void sortColors(vector<int>& nums) {\n int r = 0, g = 0, b = 0;\n for (int n : nums) {\n if (n == 0) {\n nums[b++] = 2;\n nums[g++] = 1;\n nums[r++] = 0;\n } else if (n == 1) {\n nums[b++] = 2;\n nums[g++] = 1;\n } else nums[b++] = 2;\n }\n }\n};\n"
2572-
},
2573-
{
2574-
"language": "py",
2575-
"text": "\nclass Solution:\n def sortColors(self, nums: List[int]) -> None:\n \"\"\"\n Do not return anything, modify nums in-place instead.\n \"\"\"\n p0 = cur = 0\n p2 = len(nums) - 1\n\n while cur <= p2:\n if nums[cur] == 0:\n nums[cur], nums[p0] = nums[p0], nums[cur]\n p0 += 1\n cur += 1\n elif nums[cur] == 2:\n nums[cur], nums[p2] = nums[p2], nums[cur]\n p2 -= 1\n else:\n cur += 1\n\n"
2576-
},
2577-
{
2578-
"language": "py",
2579-
"text": "\nclass Solution:\n def partition(self, head: ListNode, x: int) -> ListNode:\n l1 = cur = head\n while cur:\n if cur.val < x:\n cur.val, l1.val = l1.val, cur.val\n l1 = l1.next\n cur = cur.next\n return head\n"
2580-
}
2581-
]
2533+
"code": []
25822534
},
25832535
"subsets":{
25842536
"id": "78",

0 commit comments

Comments
 (0)