Skip to content

Commit dccbb14

Browse files
committed
fix: lint err
1 parent ce38ea3 commit dccbb14

File tree

5 files changed

+136
-141
lines changed

5 files changed

+136
-141
lines changed
Lines changed: 70 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,101 @@
11
class Heap {
22
constructor(compare) {
3-
this.data = [];
4-
this.compare = compare;
3+
this.data = [];
4+
this.compare = compare;
55
}
6-
6+
77
size() {
8-
return this.data.length;
8+
return this.data.length;
99
}
10-
10+
1111
peek() {
12-
return this.data[0];
12+
return this.data[0];
1313
}
14-
14+
1515
push(val) {
16-
this.data.push(val);
17-
this._siftUp();
16+
this.data.push(val);
17+
this._siftUp();
1818
}
19-
19+
2020
pop() {
21-
const top = this.peek();
22-
const bottom = this.data.pop();
23-
if (this.data.length > 0) {
24-
this.data[0] = bottom;
25-
this._siftDown();
26-
}
27-
return top;
21+
const top = this.peek();
22+
const bottom = this.data.pop();
23+
if (this.data.length > 0) {
24+
this.data[0] = bottom;
25+
this._siftDown();
26+
}
27+
return top;
2828
}
29-
29+
3030
_siftUp() {
31-
let i = this.data.length - 1;
32-
const node = this.data[i];
33-
while (i > 0) {
34-
const parent = Math.floor((i - 1) / 2);
35-
if (this.compare(node, this.data[parent])) {
36-
this.data[i] = this.data[parent];
37-
i = parent;
38-
} else break;
39-
}
40-
this.data[i] = node;
31+
let i = this.data.length - 1;
32+
const node = this.data[i];
33+
while (i > 0) {
34+
const parent = Math.floor((i - 1) / 2);
35+
if (this.compare(node, this.data[parent])) {
36+
this.data[i] = this.data[parent];
37+
i = parent;
38+
} else break;
39+
}
40+
this.data[i] = node;
4141
}
42-
42+
4343
_siftDown() {
44-
let i = 0;
45-
const node = this.data[0];
46-
const length = this.data.length;
47-
48-
while (true) {
49-
let left = 2 * i + 1;
50-
let right = 2 * i + 2;
51-
let swap = i;
52-
53-
if (left < length && this.compare(this.data[left], this.data[swap])) {
54-
swap = left;
55-
}
56-
if (right < length && this.compare(this.data[right], this.data[swap])) {
57-
swap = right;
44+
let i = 0;
45+
const node = this.data[0];
46+
const length = this.data.length;
47+
48+
while (true) {
49+
let left = 2 * i + 1;
50+
let right = 2 * i + 2;
51+
let swap = i;
52+
53+
if (left < length && this.compare(this.data[left], this.data[swap])) {
54+
swap = left;
55+
}
56+
if (right < length && this.compare(this.data[right], this.data[swap])) {
57+
swap = right;
58+
}
59+
if (swap === i) break;
60+
61+
this.data[i] = this.data[swap];
62+
i = swap;
5863
}
59-
if (swap === i) break;
60-
61-
this.data[i] = this.data[swap];
62-
i = swap;
63-
}
64-
65-
this.data[i] = node;
64+
65+
this.data[i] = node;
6666
}
67-
}
68-
69-
var MedianFinder = function () {
67+
}
68+
69+
var MedianFinder = function() {
7070
this.small = new Heap((a, b) => a > b); // max heap
7171
this.large = new Heap((a, b) => a < b); // min heap
72-
};
73-
74-
MedianFinder.prototype.addNum = function (num) {
72+
};
73+
74+
MedianFinder.prototype.addNum = function(num) {
7575
this.small.push(num);
76-
76+
7777
if (
78-
this.small.size() > 0 &&
79-
this.large.size() > 0 &&
80-
this.small.peek() > this.large.peek()
78+
this.small.size() > 0 &&
79+
this.large.size() > 0 &&
80+
this.small.peek() > this.large.peek()
8181
) {
82-
this.large.push(this.small.pop());
82+
this.large.push(this.small.pop());
8383
}
84-
84+
8585
if (this.small.size() > this.large.size() + 1) {
86-
this.large.push(this.small.pop());
86+
this.large.push(this.small.pop());
8787
}
8888
if (this.large.size() > this.small.size() + 1) {
89-
this.small.push(this.large.pop());
89+
this.small.push(this.large.pop());
9090
}
91-
};
92-
93-
MedianFinder.prototype.findMedian = function () {
91+
};
92+
93+
MedianFinder.prototype.findMedian = function() {
9494
if (this.small.size() > this.large.size()) {
95-
return this.small.peek();
95+
return this.small.peek();
9696
}
9797
if (this.large.size() > this.small.size()) {
98-
return this.large.peek();
98+
return this.large.peek();
9999
}
100100
return (this.small.peek() + this.large.peek()) / 2;
101-
};
102-
101+
};

โ€Žinsert-interval/hsskey.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,21 @@
55
*/
66
var insert = function(intervals, newInterval) {
77
const res = [];
8-
8+
99
for (let i = 0; i < intervals.length; i++) {
10-
if (newInterval[1] < intervals[i][0]) {
11-
res.push(newInterval);
12-
return res.concat(intervals.slice(i));
13-
} else if (newInterval[0] > intervals[i][1]) {
14-
res.push(intervals[i]);
15-
} else {
16-
newInterval = [
17-
Math.min(newInterval[0], intervals[i][0]),
18-
Math.max(newInterval[1], intervals[i][1])
19-
];
20-
}
10+
if (newInterval[1] < intervals[i][0]) {
11+
res.push(newInterval);
12+
return res.concat(intervals.slice(i));
13+
} else if (newInterval[0] > intervals[i][1]) {
14+
res.push(intervals[i]);
15+
} else {
16+
newInterval = [
17+
Math.min(newInterval[0], intervals[i][0]),
18+
Math.max(newInterval[1], intervals[i][1])
19+
];
20+
}
2121
}
22-
22+
2323
res.push(newInterval);
2424
return res;
25-
};
26-
25+
};

โ€Žkth-smallest-element-in-a-bst/hsskey.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,21 @@ var kthSmallest = function(root, k) {
1616
let n = 0;
1717
const stack = [];
1818
let cur = root;
19-
19+
2020
while (cur || stack.length > 0) {
21-
while (cur) {
22-
stack.push(cur);
23-
cur = cur.left;
24-
}
25-
26-
cur = stack.pop();
27-
n += 1;
28-
if (n === k) {
29-
return cur.val;
30-
}
31-
32-
cur = cur.right;
21+
while (cur) {
22+
stack.push(cur);
23+
cur = cur.left;
24+
}
25+
26+
cur = stack.pop();
27+
n += 1;
28+
if (n === k) {
29+
return cur.val;
30+
}
31+
32+
cur = cur.right;
3333
}
34-
34+
3535
return -1;
36-
};
37-
36+
};

โ€Žlowest-common-ancestor-of-a-binary-search-tree/hsskey.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,16 @@
1414
*/
1515
var lowestCommonAncestor = function(root, p, q) {
1616
let cur = root;
17-
17+
1818
while (cur) {
19-
if (p.val > cur.val && q.val > cur.val) {
20-
cur = cur.right;
21-
} else if (p.val < cur.val && q.val < cur.val) {
22-
cur = cur.left;
23-
} else {
24-
return cur;
25-
}
19+
if (p.val > cur.val && q.val > cur.val) {
20+
cur = cur.right;
21+
} else if (p.val < cur.val && q.val < cur.val) {
22+
cur = cur.left;
23+
} else {
24+
return cur;
25+
}
2626
}
27-
27+
2828
return null;
29-
};
30-
29+
};

โ€Žmeeting-rooms/hsskey.js

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,36 @@
11
import {
22
Interval,
3-
} from '/opt/node/lib/lintcode/index.js';
4-
5-
/**
6-
* Definition of Interval:
7-
* class Interval {
8-
* constructor(start, end) {
9-
* this.start = start;
10-
* this.end = end;
11-
* }
12-
* }
13-
*/
14-
15-
export class Solution {
3+
} from '/opt/node/lib/lintcode/index.js';
4+
5+
/**
6+
* Definition of Interval:
7+
* class Interval {
8+
* constructor(start, end) {
9+
* this.start = start;
10+
* this.end = end;
11+
* }
12+
* }
13+
*/
14+
15+
export class Solution {
1616
/**
1717
* @param {Interval[]} intervals - an array of meeting time intervals
1818
* @return {boolean} - whether a person could attend all meetings
1919
*/
2020
canAttendMeetings(intervals) {
21-
// start ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ
22-
intervals.sort((a, b) => a.start - b.start);
23-
24-
// ์—ฐ์†๋œ ํšŒ์˜์˜ ์‹œ๊ฐ„ ๋น„๊ต
25-
for (let i = 1; i < intervals.length; i++) {
26-
const prev = intervals[i - 1];
27-
const curr = intervals[i];
28-
29-
if (prev.end > curr.start) {
30-
return false;
21+
// start ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ
22+
intervals.sort((a, b) => a.start - b.start);
23+
24+
// ์—ฐ์†๋œ ํšŒ์˜์˜ ์‹œ๊ฐ„ ๋น„๊ต
25+
for (let i = 1; i < intervals.length; i++) {
26+
const prev = intervals[i - 1];
27+
const curr = intervals[i];
28+
29+
if (prev.end > curr.start) {
30+
return false;
31+
}
3132
}
32-
}
33-
34-
return true;
33+
34+
return true;
3535
}
36-
}
37-
36+
}

0 commit comments

Comments
ย (0)