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)
     */
  • 相关阅读:
    python学习笔记--Django入门一 网页显示时间
    python学习笔记--easy_install和pip
    python学习笔记--Django入门0 安装dangjo
    Linux学习笔记总结--memcached配置
    python学习笔记--基础语法
    python学习笔记--导入tab键自动补全功能的配置
    Linux学习笔记总结--ssh认证登录
    ABAP 通过字段找表程序
    smod包含具体的增强(具体实施对象) / CMOD 包含一组smod编写的增强
    SE18 BADI定义 / SE19 BADI 实现
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/13773712.html
Copyright © 2011-2022 走看看