zoukankan      html  css  js  c++  java
  • 1472. Design Browser History

    package LeetCode_1472
    
    import java.util.*
    
    /**
     * 1472. Design Browser History
     * https://leetcode.com/problems/design-browser-history/description/
     *
     * 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:
    1. BrowserHistory(string homepage) Initializes the object with the homepage of the browser.
    2. void visit(string url) Visits url from the current page. It clears up all the forward history.
    3. 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.
    4. 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.
     * */
    class BrowserHistory(homepage: String) {
        /*
        * solution: two stack to save back and forward, and keep currentUrl
        * */
        val stackBack = Stack<String>()
        val stackForward = Stack<String>()
        var currentUrl = ""
    
        init {
            currentUrl = homepage
        }
    
        fun visit(url: String) {
            //clears up all the forward history
            stackForward.clear()
            stackBack.add(currentUrl)
            currentUrl = url
        }
    
        fun back(steps: Int): String {
            var steps_ = steps
            while (steps_ > 0 && stackBack.isNotEmpty()){
                //add current to forward
                stackForward.add(currentUrl)
                currentUrl = stackBack.pop()
                steps_--
            }
            return currentUrl
        }
    
        fun forward(steps: Int): String {
            var steps_ = steps
            while (steps_ > 0 && stackForward.isNotEmpty()){
                //add current to back
                stackBack.add(currentUrl)
                currentUrl = stackForward.pop()
                steps_--
            }
            return currentUrl
        }
    }
    /**
     * Your BrowserHistory object will be instantiated and called as such:
     * var obj = BrowserHistory(homepage)
     * obj.visit(url)
     * var param_2 = obj.back(steps)
     * var param_3 = obj.forward(steps)
     */
  • 相关阅读:
    Redis总结
    设计模式-单例模式
    spring5源码解读
    关于asp.net MVC 中的TryUpdateModel方法
    WebSocket在ASP.NET MVC4中的简单实现 (该文章转自网络,经尝试并未实现,请大神指点。)
    C# 新特性 dynamic的使用及扩展
    C#用反射判断一个类型是否是Nullable同时获取它的根类型(转自网络)
    C#通用类型转换 Convert.ChangeType 转自网络
    浅谈.NET反射机制的性能优化 转自网络 博客园
    浅析大型网站的架构 转自软件中国
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/13773712.html
Copyright © 2011-2022 走看看