https://leetcode.com/problems/longest-absolute-file-path/#/description
找最长的绝对路径字符长度
给:"dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext"
返回:32
stack保存当前前序路径长度
public class Solution {
public int lengthLongestPath(String input) {
Stack<Integer> stack = new Stack();
int maxLen = 0;
// 先push一个!
stack.push(0);
for (String s : input.split("\n")) {
// "\t"就是代表tab!!是一个字符
int level = s.lastIndexOf("\t") + 1;
// 因为先push了一个0.所以level + 1去比较
while (level + 1 < stack.size()) {
stack.pop();
}
// +1表示结尾要加一个"/"
int len = stack.peek() + s.length() - level + 1;
stack.push(len);
if (s.contains(".")) {
// -1表示最后一个未见后面应该没有"/"
maxLen = Math.max(maxLen, len - 1);
}
}
return maxLen;
}
}