LeetCode之Simplify Path

转载 2015年07月07日 17:32:14
/*细节模拟题。
参考自:https://github.com/soulmachine/leetcode*/
class Solution {
public:
    string simplifyPath(string path) {
        string res;
        vector<string> path_name;
        string::iterator it = path.begin();
        while(it != path.end()){
            ++it;
            string::iterator itt = find(it, path.end(), '/');
            string dir(it, itt);
            if(!dir.empty() && dir != "."){//dir为有效目录
                if(dir == ".."){//返回上级目录
                    if(!path_name.empty()) path_name.pop_back();
                }
                else{
                    path_name.push_back(dir);
                }
            }
            it = itt;
        }
        if(path_name.empty()) return "/";
        for(int i = 0; i < path_name.size(); ++i)
           res = res +  "/" + path_name[i];
        return res;
    }
};

【九度】题目1090:路径打印 && 【LeetCode】Simplify Path

1、题目1090:路径打印 时间限制:1 秒内存限制:32 兆特殊判题:否提交:1319解决:230 题目描述: 给你一串路径,譬如: a\b\c a\d\e b\cst d\ 你把这些路径中蕴含的目...

LeetCode_71---Simplify Path

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

Simplify Path Leetcode71

Given an absolute path for a file (Unix-style), simplify it.For example, path = “/home/”, => “/home...

LeetCode - 71. Simplify Path

这道题和罗马数字转换一样,题目的求解方法与背景联系地非常强。基本上的一些规则是,如果遇到.或者“”,就直接跳过;如果遇到..就将上面的一个路径回退。所以根据上面的思想,我们首先用一个Stack存储结果...

LeetCode71 Simplify Path java题解

题目: Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home...

Leetcode 71. Simplify Path

71. Simplify Path Total Accepted: 51951 Total Submissions: 235633 Difficulty: Medium Given an...

Simplify Path -- LeetCode

原题链接: http://oj.leetcode.com/problems/simplify-path/  这道题目是Linux内核中比较常见的一个操作,就是对一个输入的文件路径进行简化。思路比较明...

Leetcode135: Simplify Path

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

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

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

leetcode笔记--Simplify Path

题目:难度(Medium) Given an absolute path for a file (Unix-style), simplify it. For example, path = "/hom...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode之Simplify Path
举报原因:
原因补充:

(最多只允许输入30个字)