71. Simplify Path

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

For example,
path="/home/", =>"/home"
path="/a/./b/../../c/", =>"/c"

S: stack O(n)

/..表示回到parent path, /.表示current path

注意corner case://, /.. /, 多个//等

    string simplifyPath(string path) {
        stack<string> stk;
        string result = "";
        int start = 0;
        while (start < path.size()) {
            if (path[start] == '/') {
                start++;
                continue;
            }
            int end = start;
            while (path[end] != '/' && end < path.size()) {
                end++;
            }
            string s = path.substr(start, end - start);
            if (s == ".." && !stk.empty()) {
                stk.pop();
            } else if (s != "." && s != "..") {
                stk.push(s);
            } 
            start = end + 1;
        }
        while (!stk.empty()) {
            result = '/' + stk.top() + result;
            stk.pop();
        }
        return result.size() == 0? "/": result;
    }

388. Longest Absolute File Path

Suppose we abstract our file system by a string in the following manner:

The string"dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext"represents:

dir
    subdir1
    subdir2
        file.ext

The directorydircontains an empty sub-directorysubdir1and a sub-directorysubdir2containing a filefile.ext.

The string"dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext"represents:

dir
    subdir1
        file1.ext
        subsubdir1
    subdir2
        subsubdir2
            file2.ext

The directorydircontains two sub-directoriessubdir1andsubdir2.subdir1contains a filefile1.extand an empty second-level sub-directorysubsubdir1.subdir2contains a second-level sub-directorysubsubdir2containing a filefile2.ext.

We are interested in finding the longest (number of characters) absolute path to a file within our file system. For example, in the second example above, the longest absolute path is"dir/subdir2/subsubdir2/file2.ext", and its length is32(not including the double quotes).

Given a string representing the file system in the above format, return the length of the longest absolute path to file in the abstracted file system. If there is no file in the system, return0.

Note:

  • The name of a file contains at least a . and an extension.

  • The name of a directory or sub-directory will not contain a ..Time complexity required:O(n)wherenis the size of the input string.

Notice thata/aa/aaa/file1.txtis not the longest file path, if there is another pathaaaaaaaaaaaaaaaaaaaaa/sth.png.

S1: stack O(n)

\n表示回到根目录,\t表示进入子目录. stk1记录当前路径,stk2记录回到根目录时pop的子目录,以便下次\t访问。每次遇到\t都从stk2拿top到stk1

//此算法并不优秀,应该还有提升空间
    int lengthLongestPath(string input) {
        int maxLen = 0;
        stack<int> stk1;
        stack<int> stk2;
        stk1.push(-1);
        int curLen = 0;
        bool isFile = false;
        for (int i = 0; i < input.size(); ++i) {
            if (input[i] == '\t' || input[i] == '\n') {
                if (curLen > 0) {
                    curLen = stk1.top() + curLen + 1;

                    if (isFile) {
                        maxLen = max(maxLen, curLen);
                    } else {
                        stk1.push(curLen);
                    }
                    isFile = false; 
                    curLen = 0;
                }
                if (input[i] == '\n') {
                    while (stk1.size() > 1) {
                        stk2.push(stk1.top());
                        stk1.pop();
                    }
                } else if (!stk2.empty()) {
                    stk1.push(stk2.top());
                    stk2.pop();
                } 
            } else {
                if (curLen > 0 && input[i] == '.') {
                    isFile = true;
                }
                curLen++;
            }
        }
        if (curLen > 0 && isFile) {
            maxLen = max(maxLen, stk1.top() + curLen + 1);
        }
        return maxLen;
    }

results matching ""

    No results matching ""