Skip to content

Instantly share code, notes, and snippets.

@bhaveshmunot1
Created June 7, 2020 21:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bhaveshmunot1/346967acbb2afadc8a7b826533572a93 to your computer and use it in GitHub Desktop.
Save bhaveshmunot1/346967acbb2afadc8a7b826533572a93 to your computer and use it in GitHub Desktop.
Leetcode #1472: Design Browser History
class BrowserHistory {
stack<string> history;
stack<string> future;
string currentWebsite;
void clearForwardStack() {
future = stack<string>();
}
void goBackOneStep() {
if (!history.empty()) {
future.push(currentWebsite);
currentWebsite = history.top();
history.pop();
}
}
void goForwardOneStep() {
if (!future.empty()) {
history.push(currentWebsite);
currentWebsite = future.top();
future.pop();
}
}
public:
BrowserHistory(string homepage) {
currentWebsite = homepage;
clearForwardStack();
}
void visit(string url) {
history.push(currentWebsite);
currentWebsite = url;
clearForwardStack();
}
string back(int steps) {
while(steps > 0) {
goBackOneStep();
steps--;
}
return currentWebsite;
}
string forward(int steps) {
while(steps > 0) {
goForwardOneStep();
steps--;
}
return currentWebsite;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment