zoukankan      html  css  js  c++  java
  • Daily Coding Problem: Problem #713

    import java.util.*
    
    /**
    This problem was asked by Quora.
    Given an absolute pathname that may have . or .. as part of it, return the shortest standardized path.
    For example, given "/usr/bin/../bin/./scripts/../", return "/usr/bin/".
     * */
    class Problem_713 {
        /*
        * solution: Stack, Time:O(n), Space:O(n)
        * */
        fun simplifyPath(path: String): String {
            if (path == null || path.isEmpty()) {
                return ""
            }
            val list = path.split("/")
            val stack = Stack<String>()
            for (item in list) {
                if (item != "." && item != "..") {
                    stack.push(item)
                } else if (stack.isNotEmpty() && item == "..") {
                    stack.pop()
                }
            }
            return stack.joinToString("/")
        }
    }
  • 相关阅读:
    HGE tutorial04
    HGE tutorial03
    HGE tutorial02 plus
    HGE tutorial02
    C 语言实例
    C 语言实例
    C 语言实例
    C 语言实例
    C 语言实例
    C 语言实例
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/13972367.html
Copyright © 2011-2022 走看看