题目:
题解:
#define MAX(a, b) ((a) > (b) ? (a) : (b))
int lengthLongestPath(char * input){
int n = strlen(input);
int pos = 0;
int ans = 0;
int * level = (int *)malloc(sizeof(int) * (n + 1));
memset(level, 0, sizeof(int) * (n + 1));
while (pos < n) {
/* 检测当前文件的深度 */
int depth = 1;
while (pos < n && input[pos] == '\t') {
pos++;
depth++;
}
/* 统计当前文件名的长度 */
bool isFile = false;
int len = 0;
while (pos < n && input[pos] != '\n') {
if (input[pos] == '.') {
isFile = true;
}
len++;
pos++;
}
/* 跳过当前的换行符 */
pos++;
if (depth > 1) {
len += level[depth - 1] + 1;
}
if (isFile) {
ans = MAX(ans, len);
} else {
level[depth] = len;
}
}
free(level);
return ans;
}