-
Notifications
You must be signed in to change notification settings - Fork 3
/
1472.cpp
44 lines (39 loc) · 1.06 KB
/
1472.cpp
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
class BrowserHistory {
public:
stack<string> forwardST;
stack<string> backwardST;
BrowserHistory(string homepage) {
forwardST.push(homepage);
}
void visit(string url) {
while (!backwardST.empty()) {
backwardST.pop();
}
forwardST.push(url);
}
string back(int steps) {
while (steps && forwardST.size() > 1) {
string current = forwardST.top();
forwardST.pop();
backwardST.push(current);
steps--;
}
return forwardST.top();
}
string forward(int steps) {
while (steps && !backwardST.empty()) {
string current = backwardST.top();
backwardST.pop();
forwardST.push(current);
steps--;
}
return forwardST.top();
}
};
/**
* 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);
*/