-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdesign-browser-history.js
58 lines (44 loc) · 1.46 KB
/
design-browser-history.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// My Solution
203 ms 51.3 MB
var BrowserHistory = function(homepage) {
this.history = [homepage], // leetcode
this.index = 0
};
BrowserHistory.prototype.visit = function(url) {
if(this.index != this.history.length-1) this.history.splice(this.index+1)
this.history.push(url)
this.index = this.history.length-1 //[leetcode,google,facebook,linkedin]
};
BrowserHistory.prototype.back = function(steps) {
for(let i = 0;i< steps; i++){
if(this.index==0) break;
this.index--
}
return this.history[this.index]
};
BrowserHistory.prototype.forward = function(steps) {
for(let i = 0;i< steps; i++){
if(this.index==this.history.length-1) break;
this.index++
}
return this.history[this.index]
};
// Other Solution - more methods but slower
329 ms 51.7 MB
var BrowserHistory = function(homepage) {
this.history = [homepage], // leetcode
this.index = 0 //3
};
BrowserHistory.prototype.visit = function(url) {
if(this.index != this.history.length-1) this.history.splice(this.index+1)
this.history.push(url) //linkedin
this.index = this.history.length-1 //[leetcode,google,facebook,linkedin]
};
BrowserHistory.prototype.back = function(steps) {
this.index = Math.max(0,this.index-steps)
return this.history[this.index] //1
};
BrowserHistory.prototype.forward = function(steps) {
this.index = Math.min(this.history.length-1,this.index+steps)
return this.history[this.index] //3
};