首页 > 其他分享 >1472. Design Browser History 前进、后退的浏览网页历史

1472. Design Browser History 前进、后退的浏览网页历史

时间:2022-10-03 03:44:05浏览次数:39  
标签:facebook back 1472 Design forward steps history com Browser

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"

用两个stack就能轻松解决,这还真没想到

class BrowserHistory {
    
    Stack<String> history = new Stack<>();
    Stack<String> future = new Stack<>();
    public BrowserHistory(String homepage) {
        history.add(homepage);
        future.clear();
    }
    
    public void visit(String url) {
        history.add(url);
        future.clear();
    }
    
    public String back(int steps) {
        while (steps > 0 && history.size() > 1) {
            future.add(history.pop());
            steps--;
        }
        return history.peek();
    }
    
    public String forward(int steps) {
        while (steps > 0 && future.size() > 0) {
            history.add(future.pop());
            steps--;
        }
        return history.peek();
    }
}

/**
 * Your BrowserHistory object will be instantiated and called as such:
 * BrowserHistory obj = new BrowserHistory(homepage);
 * obj.visit(url);
 * String param_2 = obj.back(steps);
 * String param_3 = obj.forward(steps);
 */

 



标签:facebook,back,1472,Design,forward,steps,history,com,Browser
From: https://www.cnblogs.com/immiao0319/p/16749896.html

相关文章

  • powerdesigner外键不显示名称
    powerdesigner默认外键展示如下:  如果我们想要在外键显示名称,如下:  需要如下操作: ......
  • 悉尼大学 Innovation Through Design
    week1-2 IntroductiontodesignthinkingdesignDesignthinking:Matchingneedswithfeasibletechnologyandviablebusinessstrategyandconvertintocus......
  • Ant Design Vue 在表格中插入图片
    这两天一直在用Antdv做一些小demo,今天在做表格的时候想在表格中插入图片,简单翻了下文档和国内的博客,发现所有的方法竟然都不好使,最后还是在官网的示例代码中看到相关的部......
  • PowerDesigner提示Could not Initialize JavaVM!
    PowerDesigner逆向mysql数据库报CouldnotInitializeJavaVM!提示,是因为powerdesigner基于32位JVM的原因。解决办法:安装32位的JDK或者安装mysql的ODBC驱动(重要提示:务......
  • '$.browser.msie' 为空或不是对象
    最近决定整改一下jquery的版本,于是就将jquery从1.7.2升级到了1.9.1结果就发现原有的插件报错了。'$.browser.msie'为空或不是对象,这个是jQuery错误出现这个错误,是......
  • Ant Design Vue 在表格中插入图片
    这两天一直在用Antdv做一些小demo,今天在做表格的时候想在表格中插入图片,简单翻了下文档和国内的博客,发现所有的方法竟然都不好使,最后还是在官网的示例代码中看到相关的......
  • LeetCode 2296. Design a Text Editor
    原题链接在这里:https://leetcode.com/problems/design-a-text-editor/题目:Designatexteditorwithacursorthatcandothefollowing:Add texttowherethecu......
  • React+hook+ts+ant design封装一个input和select搜索的组件
    前言我是歌谣我有个兄弟巅峰的时候排名c站总榜19叫前端小歌谣曾经我花了三年的时间创作了他现在我要用五年的时间超越他今天又是接近兄弟的一天人生难免坎坷大不了从......
  • React+hook+ts+ant design封装一个table的组件
    前言我是歌谣我有个兄弟巅峰的时候排名c站总榜19叫前端小歌谣曾经我花了三年的时间创作了他现在我要用五年的时间超越他今天又是接近兄弟的一天人生难免坎坷大不了从......
  • AntDesignVue之a-bale
    a-tableAPI描述。参数说明类型pagination分页器,参考配置项或pagination文档,设为false时不展示和进行分页Object说明pagination是个对象或false.如果......