longest-absolute-file-path

https://leetcode.com/problems/longest-absolute-file-path/

public class Solution {
    public int lengthLongestPath(String input) {
        Stack<Integer> stk = new Stack<Integer>();
        int left = 0;
        int curStrlen = 0;
        int curTab = 0;
        int ret = 0;
        boolean hasDot = false;
        boolean isFile = false;

        for (int i=0; i<input.length(); i++) {
            // \n
            if (input.charAt(i) == 10) {

                curStrlen = i-left;
                if (!stk.empty()) {
                    curStrlen += 1 + stk.peek();
                }

                if (isFile && curStrlen > ret) {
                    ret  = curStrlen;
                }
                stk.push(curStrlen);
                hasDot = false;
                isFile = false;
                curTab = 0;
                left = -1;
            }
            // \t
            else if (input.charAt(i) == 9 && curTab < stk.size()) {
                curTab++;
            }
            else if (input.charAt(i) == ‘ ‘ && i+3 < input.length() &&
                     input.substring(i, i+4).equals("    ") && curTab < stk.size()) {
                curTab++;
                i += 3;
            }
            else {
                if (left == -1) {
                    int stkLen = stk.size();
                    for (int j=0; j<stkLen-curTab; j++) {
                        stk.pop();
                    }
                    left = i;
                }
                if (input.charAt(i) == ‘.‘) {
                    hasDot = true;
                }
                else {
                    if (hasDot) {
                        isFile = true;
                    }
                }
            }

        }

        curStrlen = input.length()-left;
        if (!stk.empty()) {
            curStrlen += 1 + stk.peek();
        }
        if (isFile && curStrlen > ret) {
            ret = curStrlen;
        }
        return ret;
    }
}
时间: 2024-10-03 23:28:57

longest-absolute-file-path的相关文章

算法比赛(1)----Longest Absolute File Path

Leetcode算法比赛题目之:Longest Absolute File Path 原文链接:https://leetcode.com/contest/1/problems/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" rep

LeetCode 388. Longest Absolute File Path

原题链接在这里:https://leetcode.com/problems/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 dire

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 directory dir contains an empty sub-directory subdir1 and a sub-dire

Longest Absolute File Path -- LeetCode

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 directory dir contains an empty sub-directory subdir1 and a sub-directory 

Leetcode: 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 directory dir contains an empty sub-directory subdir1 and a sub-directory

ueditor百度富文本编辑器linux下报错: class path resource [config.json] cannot be resolved to absolute file path because it does not reside in the file system

具体报错信息如下 java.io.FileNotFoundException: class path resource [config.json] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/home/java/wcq-new-server/wcq/wcq-admin/target/wcq-admin.jar!/BOOT-INF/classes!

项目windows运行正常,而Linux上运行报错: class path resource [kwhRules.json] cannot be resolved to absolute file path because it does not reside in the file system

java.io.FileNotFoundException:class path resource [kwhRules.json] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/root/app/target/app.jar!/BOOT-INF/classes!/kwhRules.json 更改前代码: void initRules() throw

[LintCode] System Longest 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 directory dir contains an empty sub-directory subdir1 and a sub-directory 

java.nio.file.Path

public interface Path extends Comparable<Path>, Iterable<Path>, Watchable 1. A Path represents a path that is hierarchical and composed of a sequence of directory and file name elements separated by a special separator or delimiter. 2. A root

安卓各文件存储路径汇总(Android file path)

写下来,省得以后不记得到处翻: Environment.getDataDirectory() = /data Environment.getDownloadCacheDirectory() = /cache Environment.getExternalStorageDirectory() = /mnt/sdcard Environment.getExternalStoragePublicDirectory(“test”) = /mnt/sdcard/test Environment.getRo