首页 > 代码库 > longest-absolute-file-path
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; }}
longest-absolute-file-path
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。