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"

简化linux文件路径,”.”代表当前目录,”..”代表上级目录 
大体做法:先把path按’/’符分割保存起来(由于c++ string没有split函数,自己写一个吧) 
再把切割后的字符串扫一遍即可。 
具体如下

class Solution {
public:
    string simplifyPath(string path) {
        string ans;
        if(path.empty()) return ans;
        auto split = [=](char sep = ‘/‘)->vector<string> {
            string temp;
            vector<string> res;
            size_t start = 0, end = 0;
            while((end = path.find(sep, start)) != string::npos) {
                temp = path.substr(start, end - start);
                if(temp != "") res.emplace_back(temp);
                start = end + 1;
            }
            temp = path.substr(start);
            if(temp != "") res.emplace_back(temp);
            return res;
        };
        vector<string> res = split();
        vector<string> stk;
        for(auto &r: res) {
            if(r ==".") continue;
            else if(r == "..") {
                if(!stk.empty()) stk.pop_back();
            } else {
                stk.push_back(r);
            }
        }
        if(stk.empty()) {
            ans.push_back(‘/‘);
        } else {
            for(auto &r: stk) {
                ans.push_back(‘/‘);
                ans += r;
            }
        }
        return ans;
    }
};
时间: 2024-12-14 05:47:22

leetcode 71 Simplify Path的相关文章

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"

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

【一天一道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/", =

[LeetCode][Java] 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 the case w