Skip to content

Commit c163adf

Browse files
authored
Create design-browser-history.cpp
1 parent 12facde commit c163adf

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

C++/design-browser-history.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Time: ctor : O(1)
2+
// visit : O(1)
3+
// back : O(1)
4+
// foward: O(1)
5+
// Space: O(n)
6+
7+
class BrowserHistory {
8+
public:
9+
BrowserHistory(string homepage)
10+
: history_(1, homepage)
11+
, curr_{0} {
12+
13+
}
14+
15+
void visit(string url) {
16+
history_.resize(++curr_);
17+
history_.emplace_back(url);
18+
}
19+
20+
string back(int steps) {
21+
curr_ = max(curr_ - steps, 0);
22+
return history_[curr_];
23+
}
24+
25+
string forward(int steps) {
26+
curr_ = min(curr_ + steps, int(history_.size()) - 1);
27+
return history_[curr_];
28+
}
29+
30+
private:
31+
vector<string> history_;
32+
int curr_;
33+
};

0 commit comments

Comments
 (0)