1472. Design Browser History
https://leetcode.com/problems/design-browser-history/
You have a browser of one tab where you start on the homepage
and you can visit another url
, get back in the history number of steps
or move forward in the history number of steps
.
Implement the BrowserHistory
class:
BrowserHistory(string homepage)
Initializes the object with thehomepage
of the browser.void visit(string url)
visitsurl
from the current page. It clears up all the forward history.string back(int steps)
Movesteps
back in history. If you can only returnx
steps in the history andsteps > x
, you will return onlyx
steps. Return the currenturl
after moving back in history at moststeps
.string forward(int steps)
Movesteps
forward in history. If you can only forwardx
steps in the history andsteps > x
, you will forward onlyx
steps. Return the currenturl
after forwarding in history at moststeps
.
Example:
Constraints:
1 <= homepage.length <= 20
1 <= url.length <= 20
1 <= steps <= 100
homepage
andurl
consist of '.' or lower case English letters.At most
5000
calls will be made tovisit
,back
, andforward
.
模拟浏览器的访问,前进和后退操作,back(int step)把浏览记录后退step步,forward(int step)是前进step步,visit(url)会浏览url并清除所有它前面的网页。根据题意相当于在array上维持一个指针p不断跳,当调用visit时把p后插入新的url并把之后的清空。为了实现O(1)清空效果可以维持指针e指向当前上界。
Last updated
Was this helpful?