-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path106DesigeBrowserHistory.cpp
More file actions
67 lines (54 loc) · 1.49 KB
/
Copy path106DesigeBrowserHistory.cpp
File metadata and controls
67 lines (54 loc) · 1.49 KB
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
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class BrowserHistory {
vector<string> site;
int current;
int last;
public:
BrowserHistory(string homepage) {
site.push_back(homepage);
current = 0;
last = 0;
}
void visit(string url) {
current++;
if(current < (int)site.size()){
site[current] = url;
}
else{
site.push_back(url);
}
last = current;
}
string back(int steps) {
current = max(0, current - steps);
return site[current];
}
string forward(int steps) {
current = min(last, current + steps);
return site[current];
}
};
int main() {
BrowserHistory browser("leetcode.com");
browser.visit("google.com");
browser.visit("facebook.com");
browser.visit("youtube.com");
cout << browser.back(1) << endl; // facebook.com
cout << browser.back(1) << endl; // google.com
cout << browser.forward(1) << endl; // facebook.com
browser.visit("linkedin.com");
cout << browser.forward(2) << endl; // linkedin.com
cout << browser.back(2) << endl; // google.com
cout << browser.back(7) << endl; // leetcode.com
return 0;
}
/**
* 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);
*/