-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDesignBrowserHistory.java
45 lines (39 loc) · 1.16 KB
/
DesignBrowserHistory.java
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
/*https://leetcode.com/problems/design-browser-history/*/
class BrowserHistory {
Webpage browserStart, browserEnd;
public BrowserHistory(String homepage) {
browserStart = browserEnd = new Webpage(homepage);
}
public void visit(String url) {
browserEnd.next = new Webpage(url);
browserEnd.next.prev = browserEnd;
browserEnd = browserEnd.next;
}
public String back(int steps) {
while (steps-- > 0 && browserEnd != browserStart)
browserEnd = browserEnd.prev;
return browserEnd.address;
}
public String forward(int steps) {
while (steps-- > 0 && browserEnd.next != null)
browserEnd = browserEnd.next;
return browserEnd.address;
}
class Webpage
{
Webpage prev;
String address;
Webpage next;
Webpage(String address)
{
this.address = address;
}
}
}
/**
* Your BrowserHistory object will be instantiated and called as such:
* BrowserHistory obj = new BrowserHistory(homepage);
* obj.visit(url);
* String param_2 = obj.back(steps);
* String param_3 = obj.forward(steps);
*/