Longest Absolute File Path

题目
略, 太长了,who cares

答案

class Solution {
    public int lengthLongestPath(String ss) {
        Stack stk = new Stack();

        int curr = 0, max_len = 0, num_indent = 0, curr_len = 0;
        boolean new_line = true;
        while(curr < ss.length()) {
            if(ss.substring(curr, curr + 1).equals("\n")) {
                new_line = true;
                num_indent = 0;

                // How many \t after \n, consume all of them
                curr += 1;
                String token = ss.substring(curr, curr + 1);
                while(token.equals("\t")) {
                    num_indent++;
                    curr += 1;
                    token = ss.substring(curr, curr + 1);
                }
            }

            else {
                int backslash_pos = ss.indexOf("\n", curr);
                if(backslash_pos == -1) backslash_pos = ss.length();
                String token = ss.substring(curr, backslash_pos);

                if(new_line) {
                    new_line = false;
                    while(stk.size() > num_indent) {
                        curr_len = curr_len - stk.pop();
                    }
                }
                // Is this a file or directory ?
                if(token.contains(".")) {
                    max_len = Math.max(max_len, curr_len + token.length() + stk.size());
                }
                else {
                    stk.add(token.length());
                    curr_len += token.length();
                }

                curr = curr + token.length();
            }
        }
        return max_len;
    }
}

你可能感兴趣的:(Longest Absolute File Path)