LeetCode OJ Simplify Path

来源:互联网 发布:淘宝怎么卖二手闲置 编辑:程序博客网 时间:2024/06/03 21:19

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 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".
class Solution {public:    string simplifyPath(string path) {        vector<string> fileName;        for (int i = 0; i < path.size(); i++) {            if (path[i] == '/') {                if (i < path.size() - 1 && path[i + 1] == '/') {  // situation for "//"                    i += 1 - 1;                    continue;                }                if ((i == path.size() - 2 && path[i + 1] == '.') || (i < path.size() - 2 && path[i + 1] == '.' && path[i + 2] == '/')) {  // situation for "/."                    i += 2 - 1;                    continue;                }                if ((i == path.size() - 3 && path[i + 1] == '.' && path[i + 2] == '.') || (i < path.size() - 3 && path[i + 1] == '.' && path[i + 2] == '.' && path[i + 3] == '/')) {  // situation for "/.."                    i += 3 - 1;                    if (fileName.size() > 0) {                        fileName.pop_back();                        }                    continue;                }                string name;  // if the situation is not above, it is a valid file name                int j = i + 1;                for (; j < path.size() && path[j] != '/'; j++) {                    name.push_back(path[j]);                }                if (name.size() != 0) fileName.push_back(name);                i = j - 1;                continue;            }        }        string ans;        for (int i = 0; i < fileName.size(); i++) {            ans += "/" + fileName[i];        }        if (ans.size() == 0) ans = "/";        return ans;    }};


0 0
原创粉丝点击