zoukankan      html  css  js  c++  java
  • Simplify Path——LeetCode

    Given an absolute path for a file (Unix-style), simplify it.

    For example,
    path = "/home/", => "/home"
    path = "/a/./b/../../c/", => "/c"

    题目大意:给一个String,表示一个文件的绝对路径,简化它。

    解题思路:用一个栈表示即可,..向上返回一层,就是出栈一个元素,.不做操作,其他的入栈。

    public class Solution {
        public String simplifyPath(String path) {
            if (path == null || path.length() == 0) {
                return null;
            }
            Deque<String> deque = new ArrayDeque<>();
            String[] dirs = path.split("/");
            for (String dir : dirs) {
                if (".".equals(dir)){
                    continue;
                }
                else if ("..".equals(dir)) {
                    if (!deque.isEmpty()) {
                        deque.pop();
                    }
                } else {
                    if(dir==null||dir.length()==0){
                        continue;
                    }
                    deque.push(dir);
                }
            }
            String res = "/";
            while (!deque.isEmpty()) {
                res += deque.pollLast();
                res += "/";
            }
            res = res.substring(0, res.length() > 1 ? res.length() - 1 : 1);
            return res;
        }
    }
  • 相关阅读:
    持续集成概念
    性能测试,负载测试,压力测试有什么区别
    安全测试
    接口测试及常用接口测试工具
    python-Csv 实战
    Python3 + Appium学习链接
    python-Txt实践
    python-ddt实践
    保险--总结
    selenium与页面的交互
  • 原文地址:https://www.cnblogs.com/aboutblank/p/4599762.html
Copyright © 2011-2022 走看看