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)
     */
  • 相关阅读:
    渲染机制
    CSS渲染速度改善的十个方法与建议
    HTML你应该知道的三大基本元素
    链接属性rel=’external’、rel=’nofollow’、rel=’external nofollow’三种写法的区别
    rel=nofollow
    CSS3 圆形时钟式网页进度条
    @media screen解决移动web开发的多分辨率问题
    meta 标签属性(网站兼容与优化需要)
    如何解决谷歌浏览器下jquery无法获取图片的尺寸
    CSS设计之页面滚动条出现时防止页面跳动的方法
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/13773712.html
Copyright © 2011-2022 走看看