zoukankan      html  css  js  c++  java
  • [LeetCode] 1472. 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 the homepage of the browser.
    • void visit(string url) Visits url from the current page. It clears up all the forward history.
    • string back(int steps) Move steps back in history. If you can only return x steps in the history and steps > x, you will return only x steps. Return the current url after moving back in history at most steps.
    • string forward(int steps) Move steps forward in history. If you can only forward x steps in the history and steps > x, you will forward only x steps. Return the current url after forwarding in history at most steps.

    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 and url consist of  '.' or lower case English letters.
    • At most 5000 calls will be made to visitback, and forward.

    设计浏览器历史记录。

    你有一个只支持单个标签页的 浏览器 ,最开始你浏览的网页是 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是不会删除元素的,只会修改或者增加。

    visit()
    如果当前的index + 1小于list.size(),说明list长度更大,只能在对应下标修改,把新visit的url放入index + 1的位置,同时size要变化。
     
    back()
    forward()
    这两个函数其实思路类似,都是去取size范围内的一个正确的index。back() 找的是size范围内最小的但是不小于0的index,也就是说无论后退几步,最多只能后退到homepage。forward() 找的是size范围内最大的页面的index但是不能大于size(有效的范围),所以取的是 size - 1 和 index + steps 的较小值。
    时间O(1)
    空间O(n)
    Java实现
     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  */
  • 相关阅读:
    mybatis映射文件配置-1对多
    启动服务器加载spring.xml:web.xml配置
    spring-mybatis.xml配置文件
    json转换器映射文件配置
    Restful levels
    事务隔离级别「脏读、不可重复读、幻读」
    Java CDI
    session和Cookie的介绍和应用
    java RE Validation常用
    hello2 source Analysis
  • 原文地址:https://www.cnblogs.com/cnoodle/p/14478021.html
Copyright © 2011-2022 走看看