I hate my solution!


  • 0
    H

    My solution is too complicate. With the help of some built-in method such as split(), lastIndexOf(), I don't have to do everything myself.

    I hate my code T3T

    public class Solution {
        // constant
        private final char SPLITTER = '\n';
        private final char TAB = '\t';
        private final String POINT = ".";
        // environment
        private char[] path = new char[0];          // input path
        private int len = 0;                        // length of input path
        private int p = 0;                          // pointer to the path
        // init enviroment
        private void init(String s) {
            path = s.toCharArray();                 // input path
            len = s.length();                       // length of input path
            p = 0;                                  // pointer
        }
        // assert: the input path should be valid path.
        public int lengthLongestPath(String input) {
            // defence
            if (input.length() == 0) { return 0; }
            // init environment
            init(input);
            // local variable
            int[] stack = new int[len];             // note length of substrings
            boolean[] isFile = new boolean[len];    // note if substrings in the stack are files
            int currDepth = -1;                     // root has depth 0
            int max = 0;                            // result
            // iterate sub path
            do {
                int depth = nextDepth();
                String subpath = nextSubPath();
                if (depth <= currDepth) {
                    max = Math.max(max,collect(stack,isFile,currDepth));
                    while (depth <= currDepth) { --currDepth; }
                }
                stack[++currDepth] = subpath.length();
                if (currDepth > 0) { ++stack[currDepth]; } // count slash
                isFile[currDepth] = isFile(subpath);
            } while (hasNext());
            max = Math.max(max,collect(stack,isFile,currDepth));
            return max;
        }
        /* have more sub path? if yes, move pointer to first '\t' */
        private boolean hasNext() {
            boolean res = (p < len) && path[p] == SPLITTER;
            if (res) { ++p; }
            return res;
        }
        /* return the depth of next dir/file. move pointer after the last '\t' */
        private int nextDepth() {
            int depth = 0;
            while (p < len && path[p] == TAB) { ++depth; ++p; }
            return depth;
        }
        /* return the next dir/file. move pointer to next '\n' */
        private String nextSubPath() {
            StringBuilder sb = new StringBuilder();
            while (p < len && path[p] != SPLITTER && path[p] != TAB) { sb.append(path[p++]); }
            return sb.toString();
        }
        /* check if the given string is the name of file. all file name contains "." */
        private boolean isFile(String s) {
            return s.contains(POINT);
        }
        /* sum up the length of sub path in current stack */
        private int collect(int[] stack, boolean[] isFile, int depth) {
            int len = 0;
            if (!isFile[depth]) { return 0; }
            for (int i = 0; i <= depth; i++) {
                len += stack[i];
            }
            return len;
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.