您的位置:首页 > 其它

LeetCode 388: Longest Absolute File Path

2017-10-09 13:45 381 查看
class Solution {
public int lengthLongestPath(String input) {
if (input.length() == 0) return 0;
String[] layers = input.split("\n");
int[] stack = new int[layers.length + 1];
int result = 0;
for (String s : layers) {
int level = s.lastIndexOf("\t") + 1, current = stack[level + 1] = stack[level] + s.length() - level + 1; // Find how many \t, which mean it belongs to which level. stack[level + 1] = stack[level] + s.length() - 1evel + 1 is calculate current level length by removing \t and update the prefix for next level
if (s.contains(".")) result = Math.max(result, current - 1); // Check the last file.
}
return result;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: