|
| 1 | +<!--|This file generated by command(leetcode description); DO NOT EDIT. |--> |
| 2 | +<!--+----------------------------------------------------------------------+--> |
| 3 | +<!--|@author openset <openset.wang@gmail.com> |--> |
| 4 | +<!--|@link https://github.com/openset |--> |
| 5 | +<!--|@home https://github.com/openset/leetcode |--> |
| 6 | +<!--+----------------------------------------------------------------------+--> |
| 7 | + |
| 8 | +[< Previous](../the-k-strongest-values-in-an-array "The k Strongest Values in an Array") |
| 9 | + |
| 10 | +[Next >](../paint-house-iii "Paint House III") |
| 11 | + |
| 12 | +## [1472. Design Browser History (Medium)](https://leetcode.com/problems/design-browser-history "设计浏览器历史记录") |
| 13 | + |
| 14 | +<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> |
| 15 | + |
| 16 | +<p>Implement the <code>BrowserHistory</code> class:</p> |
| 17 | + |
| 18 | +<ul> |
| 19 | + <li><code>BrowserHistory(string homepage)</code> Initializes the object with the <code>homepage</code> of the browser.</li> |
| 20 | + <li><code>void visit(string url)</code> visits <code>url</code> from the current page. It clears up all the forward history.</li> |
| 21 | + <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> |
| 22 | + <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> |
| 23 | +</ul> |
| 24 | + |
| 25 | +<p> </p> |
| 26 | +<p><strong>Example:</strong></p> |
| 27 | + |
| 28 | +<pre> |
| 29 | +<b>Input:</b> |
| 30 | +["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"] |
| 31 | +[["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]] |
| 32 | +<b>Output:</b> |
| 33 | +[null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"] |
| 34 | + |
| 35 | +<b>Explanation:</b> |
| 36 | +BrowserHistory browserHistory = new BrowserHistory("leetcode.com"); |
| 37 | +browserHistory.visit("google.com"); // You are in "leetcode.com". Visit "google.com" |
| 38 | +browserHistory.visit("facebook.com"); // You are in "google.com". Visit "facebook.com" |
| 39 | +browserHistory.visit("youtube.com"); // You are in "facebook.com". Visit "youtube.com" |
| 40 | +browserHistory.back(1); // You are in "youtube.com", move back to "facebook.com" return "facebook.com" |
| 41 | +browserHistory.back(1); // You are in "facebook.com", move back to "google.com" return "google.com" |
| 42 | +browserHistory.forward(1); // You are in "google.com", move forward to "facebook.com" return "facebook.com" |
| 43 | +browserHistory.visit("linkedin.com"); // You are in "facebook.com". Visit "linkedin.com" |
| 44 | +browserHistory.forward(2); // You are in "linkedin.com", you cannot move forward any steps. |
| 45 | +browserHistory.back(2); // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com" |
| 46 | +browserHistory.back(7); // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com" |
| 47 | +</pre> |
| 48 | + |
| 49 | +<p> </p> |
| 50 | +<p><strong>Constraints:</strong></p> |
| 51 | + |
| 52 | +<ul> |
| 53 | + <li><code>1 <= homepage.length <= 20</code></li> |
| 54 | + <li><code>1 <= url.length <= 20</code></li> |
| 55 | + <li><code>1 <= steps <= 100</code></li> |
| 56 | + <li><code>homepage</code> and <code>url</code> consist of '.' or lower case English letters.</li> |
| 57 | + <li>At most <code>5000</code> calls will be made to <code>visit</code>, <code>back</code>, and <code>forward</code>.</li> |
| 58 | +</ul> |
| 59 | + |
| 60 | +### Related Topics |
| 61 | + [[Design](../../tag/design/README.md)] |
| 62 | + |
| 63 | +### Hints |
| 64 | +<details> |
| 65 | +<summary>Hint 1</summary> |
| 66 | +Use two stack one for back history and one for forward history and simulate the functions. |
| 67 | +</details> |
| 68 | + |
| 69 | +<details> |
| 70 | +<summary>Hint 2</summary> |
| 71 | +Can you do faster by using different data structure ? |
| 72 | +</details> |
0 commit comments