Created
June 7, 2020 21:28
-
-
Save bhaveshmunot1/346967acbb2afadc8a7b826533572a93 to your computer and use it in GitHub Desktop.
Leetcode #1472: Design Browser History
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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