码迷,mamicode.com
首页 > 其他好文 > 详细

71. Simplify Path

时间:2018-05-19 14:43:15      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:sim   bre   ret   rip   int   const   ++   http   substr   

https://leetcode.com/problems/simplify-path/description/

class Solution {
public:
    string simplifyPath(string path) {
        int idx = 0;
        vector<string> s;
        while (true) {
            string token = getToken(path, idx);
            if (token == "")    break;
            if (token == ".")   continue;
            if (token == "..") {
                if (s.size() > 0)
                    s.pop_back();
            }
            else
                s.push_back(token);
        }
        
        string res;
        for (auto& t : s) {
            res += "/" + t;
        }
        return res == "" ? "/" : res;
    }
    string getToken(const string& path, int& idx) {
        while (idx < path.length() && path[idx] == /) idx++;
        if (idx == path.length())   return "";
        int end = idx;
        while (end < path.length() && path[end] != /) end++;
        string res = path.substr(idx, end-idx);
        idx = end;
        return res;
    }
};

 

71. Simplify Path

标签:sim   bre   ret   rip   int   const   ++   http   substr   

原文地址:https://www.cnblogs.com/JTechRoad/p/9060210.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!