-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdesignBrowserHistory.cpp
More file actions
40 lines (34 loc) · 927 Bytes
/
designBrowserHistory.cpp
File metadata and controls
40 lines (34 loc) · 927 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
// Source: https://leetcode.com/problems/design-browser-history/
// Author: Miao Zhang
// Date: 2021-05-09
class BrowserHistory {
public:
BrowserHistory(string homepage): idx_(0) {
urls_.push_back(std::move(homepage));
}
void visit(string url) {
while (urls_.size() > idx_ + 1) {
urls_.pop_back();
}
idx_++;
urls_.push_back(std::move(url));
}
string back(int steps) {
idx_ = max(idx_ - steps, 0);
return urls_[idx_];
}
string forward(int steps) {
idx_ = min(idx_ + steps, int(urls_.size()) - 1);
return urls_[idx_];
}
private:
int idx_;
vector<string> urls_;
};
/**
* 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);
*/