题目

simplify-path


算法

* 直接模拟

* strtok

* stringstream


代码

* 直接模拟


class Solution {

public:

    string simplifyPath(string path) {

        vector<string> v;

        int i = 0;

        while (i < path.size()) {

            while (path[i] == '/' && i < path.size()) ++i;

            if (i == path.size()) break;

            int start = i;

            while (path[i] != '/' && i < path.size()) ++i;

            int end = i - 1;

            string s = path.substr(start, end - start + 1);

            if (s == "..") {

                if (!v.empty()) v.pop_back(); 

            } else if (s != ".") {

                v.push_back(s);

            }

        }

        if (v.empty()) return "/";

        string res;

        for (int i = 0; i < v.size(); ++i) {

            res += '/' + v[i];

        }

        return res;

    }

};

* strtok


class Solution {

public:

    string simplifyPath(string path) {

        vector<string> v;

        char *cstr = new char[path.length() + 1];

        strcpy(cstr, path.c_str());

        char *pch = strtok(cstr, "/");

        while (pch != NULL) {

            string p = string(pch);

            if (p == "..") {

                if (!v.empty()) v.pop_back();

            } else if (p != ".") {

                v.push_back(p);

            }

            pch = strtok(NULL, "/");

        }

        if (v.empty()) return "/";

        string res;

        for (int i = 0; i < v.size(); ++i) {

            res += '/' + v[i];

        }

        return res;

    }

};

* stringstream


class Solution {

public:

    string simplifyPath(string path) {

        string res, t;

        stringstream ss(path);

        vector<string> v;

        while (getline(ss, t, '/')) {

            if (t == "" || t == ".") continue;

            if (t == ".." && !v.empty()) v.pop_back();

            else if (t != "..") v.push_back(t);

        }

        for (string s : v) res += "/" + s;

        return res.empty() ? "/" : res;

    }

};

results matching ""

    No results matching ""