Class BrowserHistory
java.lang.Object
g1401_1500.s1472_design_browser_history.BrowserHistory
1472 - Design Browser History.<p>Medium</p>
<p>You have a <strong>browser</strong> of one tab where you start on the <code>homepage</code> and you can visit another <code>url</code>, get back in the history number of <code>steps</code> or move forward in the history number of <code>steps</code>.</p>
<p>Implement the <code>BrowserHistory</code> class:</p>
<ul>
<li><code>BrowserHistory(string homepage)</code> Initializes the object with the <code>homepage</code> of the browser.</li>
<li><code>void visit(string url)</code> Visits <code>url</code> from the current page. It clears up all the forward history.</li>
<li><code>string back(int steps)</code> Move <code>steps</code> back in history. If you can only return <code>x</code> steps in the history and <code>steps > x</code>, you will return only <code>x</code> steps. Return the current <code>url</code> after moving back in history <strong>at most</strong> <code>steps</code>.</li>
<li><code>string forward(int steps)</code> Move <code>steps</code> forward in history. If you can only forward <code>x</code> steps in the history and <code>steps > x</code>, you will forward only <code>x</code> steps. Return the current <code>url</code> after forwarding in history <strong>at most</strong> <code>steps</code>.</li>
</ul>
<p><strong>Example:</strong></p>
<p><strong>Input:</strong> [“BrowserHistory”,“visit”,“visit”,“visit”,“back”,“back”,“forward”,“visit”,“forward”,“back”,“back”] [[“leetcode.com”],[“google.com”],[“facebook.com”],[“youtube.com”],[1],[1],[1],[“linkedin.com”],[2],[2],[7]]</p>
<p><strong>Output:</strong> [null,null,null,null,“facebook.com”,“google.com”,“facebook.com”,null,“linkedin.com”,“google.com”,“leetcode.com”]</p>
<p><strong>Explanation:</strong></p>
<p>BrowserHistory browserHistory = new BrowserHistory(“leetcode.com”);</p>
<p>browserHistory.visit(“google.com”); // You are in “leetcode.com”. Visit “google.com”</p>
<p>browserHistory.visit(“facebook.com”); // You are in “google.com”. Visit “facebook.com”</p>
<p>browserHistory.visit(“youtube.com”); // You are in “facebook.com”. Visit “youtube.com”</p>
<p>browserHistory.back(1); // You are in “youtube.com”, move back to “facebook.com” return “facebook.com”</p>
<p>browserHistory.back(1); // You are in “facebook.com”, move back to “google.com” return “google.com”</p>
<p>browserHistory.forward(1); // You are in “google.com”, move forward to “facebook.com” return “facebook.com”</p>
<p>browserHistory.visit(“linkedin.com”); // You are in “facebook.com”. Visit “linkedin.com”</p>
<p>browserHistory.forward(2); // You are in “linkedin.com”, you cannot move forward any steps.</p>
<p>browserHistory.back(2); // You are in “linkedin.com”, move back two steps to “facebook.com” then to “google.com”. return “google.com”</p>
<p>browserHistory.back(7); // You are in “google.com”, you can move back only one step to “leetcode.com”. return “leetcode.com”</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= homepage.length <= 20</code></li>
<li><code>1 <= url.length <= 20</code></li>
<li><code>1 <= steps <= 100</code></li>
<li><code>homepage</code> and <code>url</code> consist of ‘.’ or lower case English letters.</li>
<li>At most <code>5000</code> calls will be made to <code>visit</code>, <code>back</code>, and <code>forward</code>.</li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
BrowserHistory
-
-
Method Details
-
visit
-
back
-
forward
-