mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
46 lines
3.8 KiB
HTML
46 lines
3.8 KiB
HTML
<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> </p>
|
|
<p><strong class="example">Example:</strong></p>
|
|
|
|
<pre>
|
|
<b>Input:</b>
|
|
["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]]
|
|
<b>Output:</b>
|
|
[null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"]
|
|
|
|
<b>Explanation:</b>
|
|
BrowserHistory browserHistory = new BrowserHistory("leetcode.com");
|
|
browserHistory.visit("google.com"); // You are in "leetcode.com". Visit "google.com"
|
|
browserHistory.visit("facebook.com"); // You are in "google.com". Visit "facebook.com"
|
|
browserHistory.visit("youtube.com"); // You are in "facebook.com". Visit "youtube.com"
|
|
browserHistory.back(1); // You are in "youtube.com", move back to "facebook.com" return "facebook.com"
|
|
browserHistory.back(1); // You are in "facebook.com", move back to "google.com" return "google.com"
|
|
browserHistory.forward(1); // You are in "google.com", move forward to "facebook.com" return "facebook.com"
|
|
browserHistory.visit("linkedin.com"); // You are in "facebook.com". Visit "linkedin.com"
|
|
browserHistory.forward(2); // You are in "linkedin.com", you cannot move forward any steps.
|
|
browserHistory.back(2); // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com"
|
|
browserHistory.back(7); // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com"
|
|
</pre>
|
|
|
|
<p> </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>
|