【一天一道LeetCode】#71. Simplify Path

一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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

For example,

path = “/home/”, => “/home”

path = “/a/./b/../../c/”, => “/c”

Corner Cases:

  • Did you consider the case where path = “/../”?

    In this case, you should return “/”.

  • Another corner case is the path might contain multiple slashes ‘/’ together, such as “/home//foo/”.

    In this case, you should ignore redundant slashes and return “/home/foo”.

(二)解题

题目大意:给定一个绝对路径,简化路径

注意一下几点:

1、“..”表示跳到上一层目录

2、“.”表示当前目录

3、“////”多个/当一个处理

在这里,我们可以利用stack来处理,“..”则把栈顶元素弹出,“.”则不作处理,其他的表示目录的名字就直接压栈。

具体思路可以看代码:

class Solution {
public:
    string simplifyPath(string path) {
        stack<string> pathStack;//存放目录
        path+="/";//避免处理边界,即/a这种情况
        string temp = "";//用作临时string
        for(int i = 0 ; i< path.length() ;)
        {
            if(path[i] == ‘/‘)
            {
                if(temp=="..") {//返回上一层目录
                    if(!pathStack.empty()) pathStack.pop();//如果非空则弹出,为空就不做处理
                }
                else if(temp=="."|| temp =="") {}//不做处理,当前目录
                else  pathStack.push(temp);//正常的目录
                while(i<path.length()&&path[i]==‘/‘) i++;//跳过连续的‘/’
                temp = "";//temp初始化
            }
            else
            {
                temp+=path[i++];
            }
        }
        string ret;
        if(pathStack.empty()) ret = "/";//为空时直接输出
        else {
            while(!pathStack.empty())//非空则按格式输出
            {
                string eachPath = pathStack.top();
                pathStack.pop();
                ret = "/"+eachPath + ret;
            }
        }
        return ret;
    }
};
时间: 2024-11-08 19:17:30

【一天一道LeetCode】#71. Simplify Path的相关文章

leetcode 71 Simplify Path

题目连接 https://leetcode.com/problems/simplify-path/ Simplify Path Description Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c"

leetCode 71.Simplify Path(化简路径) 解题思路和方法

Simplify Path Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", => "/home" path = "/a/./b/../../c/", => "/c" click to show corner cases. Corner Cases: Did you consider

leetcode 71 Simplify Path ------ java

Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c" click to show corner cases. Corner Cases: Did you consider the case where p

[LeetCode] 71. Simplify Path 简化路径

Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c" Corner Cases: Did you consider the case where path = "/../"?In th

LeetCode开心刷题四十八天——71. Simplify Path

71. Simplify Path Medium 5101348FavoriteShare Given an absolute path for a file (Unix-style), simplify it. Or in other words, convert it to the canonical path. In a UNIX-style file system, a period . refers to the current directory. Furthermore, a do

【LeetCode】Simplify Path

Simplify Path Given an absolute path for a file (Unix-style), simplify it. Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c"

【一天一道LeetCode】#113. Path Sum II

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22,

71. Simplify Path做题报告

题目链接: 71. Simplify Path 题目大意: 简化路径,如果对于‘.'字符,表明为此目录,对于‘..'字符,表明为回到此目录上一节点(即:删除上一节点),我们现在需要删除多余的'/'字符和'.'字符 做题报告: (1)该题涉及的算法与数据结构与知识点 Java NIO中的Files类,正则表达式,栈 (2)自己的解答思路+代码+分析时间和空间复杂度 栈 class Solution { public String simplifyPath(String path) { String

[C++]LeetCode: 117 Simplify Path (简化Unix路径 list双向链表)

题目: Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", => "/home" path = "/a/./b/../../c/", => "/c" click to show corner cases. Corner Cases: Did you consider the case w