Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1dedc2b

Browse files
feat: add js solution to lc problem: No.0731 (doocs#3572)
1 parent f82eaaa commit 1dedc2b

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

‎solution/0700-0799/0731.My Calendar II/README.md‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,43 @@ func (this *MyCalendarTwo) Book(start int, end int) bool {
208208
*/
209209
```
210210

211+
#### JavaScript
212+
213+
```js
214+
var MyCalendarTwo = function () {
215+
this.events = [];
216+
this.overlaps = [];
217+
};
218+
219+
/**
220+
* @param {number} start
221+
* @param {number} end
222+
* @return {boolean}
223+
*/
224+
MyCalendarTwo.prototype.book = function (start, end) {
225+
for (let [s, e] of this.overlaps) {
226+
if (Math.max(start, s) < Math.min(end, e)) {
227+
return false;
228+
}
229+
}
230+
231+
for (let [s, e] of this.events) {
232+
if (Math.max(start, s) < Math.min(end, e)) {
233+
this.overlaps.push([Math.max(start, s), Math.min(end, e)]);
234+
}
235+
}
236+
237+
this.events.push([start, end]);
238+
return true;
239+
};
240+
241+
/**
242+
* Your MyCalendarTwo object will be instantiated and called as such:
243+
* var obj = new MyCalendarTwo()
244+
* var param_1 = obj.book(start,end)
245+
*/
246+
```
247+
211248
<!-- tabs:end -->
212249

213250
<!-- solution:end -->

‎solution/0700-0799/0731.My Calendar II/README_EN.md‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,43 @@ func (this *MyCalendarTwo) Book(start int, end int) bool {
206206
*/
207207
```
208208

209+
#### JavaScript
210+
211+
```js
212+
var MyCalendarTwo = function () {
213+
this.events = [];
214+
this.overlaps = [];
215+
};
216+
217+
/**
218+
* @param {number} start
219+
* @param {number} end
220+
* @return {boolean}
221+
*/
222+
MyCalendarTwo.prototype.book = function (start, end) {
223+
for (let [s, e] of this.overlaps) {
224+
if (Math.max(start, s) < Math.min(end, e)) {
225+
return false;
226+
}
227+
}
228+
229+
for (let [s, e] of this.events) {
230+
if (Math.max(start, s) < Math.min(end, e)) {
231+
this.overlaps.push([Math.max(start, s), Math.min(end, e)]);
232+
}
233+
}
234+
235+
this.events.push([start, end]);
236+
return true;
237+
};
238+
239+
/**
240+
* Your MyCalendarTwo object will be instantiated and called as such:
241+
* var obj = new MyCalendarTwo()
242+
* var param_1 = obj.book(start,end)
243+
*/
244+
```
245+
209246
<!-- tabs:end -->
210247

211248
<!-- solution:end -->
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
var MyCalendarTwo = function () {
2+
this.events = [];
3+
this.overlaps = [];
4+
};
5+
6+
/**
7+
* @param {number} start
8+
* @param {number} end
9+
* @return {boolean}
10+
*/
11+
MyCalendarTwo.prototype.book = function (start, end) {
12+
for (let [s, e] of this.overlaps) {
13+
if (Math.max(start, s) < Math.min(end, e)) {
14+
return false;
15+
}
16+
}
17+
18+
for (let [s, e] of this.events) {
19+
if (Math.max(start, s) < Math.min(end, e)) {
20+
this.overlaps.push([Math.max(start, s), Math.min(end, e)]);
21+
}
22+
}
23+
24+
this.events.push([start, end]);
25+
return true;
26+
};
27+
28+
/**
29+
* Your MyCalendarTwo object will be instantiated and called as such:
30+
* var obj = new MyCalendarTwo()
31+
* var param_1 = obj.book(start,end)
32+
*/

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /