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:
Input: ["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]] Output: [null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"] Explanation: 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"
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
.
设计浏览器历史记录。
你有一个只支持单个标签页的 浏览器 ,最开始你浏览的网页是 homepage ,你可以访问其他的网站 url ,也可以在浏览历史中后退 steps 步或前进 steps 步。
请你实现 BrowserHistory 类:
BrowserHistory(string homepage) ,用 homepage 初始化浏览器类。
void visit(string url) 从当前页跳转访问 url 对应的页面 。执行此操作会把浏览历史前进的记录全部删除。
string back(int steps) 在浏览历史中后退 steps 步。如果你只能在浏览历史中后退至多 x 步且 steps > x ,那么你只后退 x 步。请返回后退 至多 steps 步以后的 url 。
string forward(int steps) 在浏览历史中前进 steps 步。如果你只能在浏览历史中前进至多 x 步且 steps > x ,那么你只前进 x 步。请返回前进 至多 steps步以后的 url 。来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/design-browser-history
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
这是一道设计题。我这里给出一个list的解法。初始化的时候我需要一个空的array list和几个变量,index表示list的下标,即当前我在哪个页面上;size表示当前list里面有效的,能访问到的范围的上限。注意这里我创建的array list是不会删除元素的,只会修改或者增加。
1 class BrowserHistory { 2 List<String> list; 3 int index; 4 int size; 5 6 public BrowserHistory(String homepage) { 7 list = new ArrayList<>(); 8 list.add(homepage); 9 index = 0; 10 size = 1; 11 } 12 13 public void visit(String url) { 14 if (index + 1 < list.size()) { 15 list.set(index + 1, url); 16 } else { 17 list.add(url); 18 } 19 index++; 20 size = index + 1; 21 } 22 23 public String back(int steps) { 24 index = Math.max(0, index - steps); 25 return list.get(index); 26 } 27 28 public String forward(int steps) { 29 index = Math.min(size - 1, index + steps); 30 return list.get(index); 31 } 32 } 33 34 /** 35 * Your BrowserHistory object will be instantiated and called as such: 36 * BrowserHistory obj = new BrowserHistory(homepage); 37 * obj.visit(url); 38 * String param_2 = obj.back(steps); 39 * String param_3 = obj.forward(steps); 40 */