-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path77_designbrowserhistory.cpp
More file actions
42 lines (37 loc) · 903 Bytes
/
77_designbrowserhistory.cpp
File metadata and controls
42 lines (37 loc) · 903 Bytes
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
//https://leetcode.com/problems/design-browser-history/description/
#define pb push_back
class BrowserHistory {
public:
int ele;
vector<string> vc;
BrowserHistory(string homepage) {
vc.pb(homepage);
ele =0;
}
void visit(string url) {
int l = vc.size()-1;
while(l>ele){
vc.pop_back();
l--;
}
ele++;
vc.pb(url);
}
string back(int steps) {
ele-=steps;
if(ele<0) ele=0;
return vc[ele];
}
string forward(int steps) {
ele +=steps;
if(ele>=vc.size()) ele = vc.size()-1;
return vc[ele];
}
};
/**
* 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);
*/