-
Notifications
You must be signed in to change notification settings - Fork 0
/
broswerHistory.cpp
55 lines (50 loc) · 1.35 KB
/
broswerHistory.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
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class BrowserHistory {
public:
vector<string> v;
int curr;
BrowserHistory(string homepage) {
v.clear();
curr=0;
v.push_back(homepage);
}
void visit(string url) {
v.push_back(url);
curr=v.size()-1;
}
string back(int steps) {
if(steps>curr){
curr=0;
return v[0];
}else{
curr=curr-steps;
return v[curr];
}
}
string forward(int steps) {
if(steps>v.size()-curr-1){
curr=v.size()-1;
return v.back();
}else{
curr=curr+steps;
return v[curr];
}
}
};
int main() {
BrowserHistory browser("leetcode.com");
browser.visit("google.com");
browser.visit("facebook.com");
browser.visit("youtube.com");
cout << browser.back(1) << endl; // should print "facebook.com"
cout << browser.back(1) << endl; // should print "google.com"
cout << browser.forward(1) << endl; // should print "facebook.com"
browser.visit("linkedin.com");
cout << browser.forward(2) << endl; // should print "linkedin.com"
cout << browser.back(2) << endl; // should print "google.com"
cout << browser.back(7) << endl; // should print "leetcode.com"
return 0;
}