首页 > 其他 > 详细

71. Simplify Path

时间:2018-05-19 14:29:38      阅读:193      评论:0      收藏:0      [点我收藏+]

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!