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 subdir2 containing a file file.ext.

The string "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext" represents:

dir
subdir1
file1.ext
subsubdir1
subdir2
subsubdir2
file2.ext

The directory dir contains two sub-directories subdir1 and subdir2subdir1 contains a file file1.ext and an empty second-level sub-directory subsubdir1subdir2contains a second-level sub-directory subsubdir2 containing a file file2.ext.

We are interested in finding the longest (number of characters) absolute path to a file within our file system. For example, in the second example above, the longest absolute path is "dir/subdir2/subsubdir2/file2.ext", and its length is 32 (not including the double quotes).

Given a string representing the file system in the above format, return the length of the longest absolute path to file in the abstracted file system. If there is no file in the system, return 0.

Note:

  • The name of a file contains at least a . and an extension.
  • The name of a directory or sub-directory will not contain a ..

Time complexity required: O(n) where n is the size of the input string.

Notice that a/aa/aaa/file1.txt is not the longest file path, if there is another path aaaaaaaaaaaaaaaaaaaaa/sth.png.

分析:用stack的size来记录level的个数,stack里面的值是每一个level对应的长度。每次存入一个已经存在的level的时候,会把以前同一个level的清空。

 public class Solution {
public int lengthLongestPath(String input) {
String[] tokens = input.split("\n");
int longestPath = , currLength = ;
Stack<Integer> stack = new Stack<>(); for (String s : tokens) {
int level = s.lastIndexOf("\t") + ; while (stack.size() > level) {
currLength -= stack.pop();
}
int length = s.replace("\t", "").length();
currLength += length;
stack.push(length); if (s.contains(".")) {
longestPath = Math.max(currLength + level, longestPath);
}
}
return longestPath;
}
}

Longest Absolute File Path的更多相关文章

  1. [LeetCode] Longest Absolute File Path 最长的绝对文件路径

    Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...

  2. Leetcode: Longest Absolute File Path

    Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...

  3. [Swift]LeetCode388. 文件的最长绝对路径 | Longest Absolute File Path

    Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...

  4. Leetcode算法比赛----Longest Absolute File Path

    问题描述 Suppose we abstract our file system by a string in the following manner: The string "dir\n ...

  5. Longest Absolute File Path -- LeetCode

    Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...

  6. [LeetCode] 388. Longest Absolute File Path 最长的绝对文件路径

    Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...

  7. 【leetcode】388. Longest Absolute File Path

    题目如下: Suppose we abstract our file system by a string in the following manner: The string "dir\ ...

  8. 【LeetCode】388. Longest Absolute File Path 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 日期 题目地址:https://leetcode. ...

  9. 388. Longest Absolute File Path

    就是看哪个文件的绝对路径最长,不是看最深,是看最长,跟文件夹名,文件名都有关. \n表示一波,可能存在一个文件,可能只有文件夹,但是我们需要检测. 之后的\t表示层数. 思路是如果当前层数多余已经有的 ...

随机推荐

  1. 错误"ORA-04091: table is mutating, trigger/function may not see it"的原因以及解决办法

    错误的原因该错误是在编写trigger时常遇到的问题,其根本原因是由于对本表的操作造成的.对于使用了for each row 的触发器,做了DML操作(delete,update,insert),还没 ...

  2. gradle项目中如何支持java与scala混合使用?

    之前写过一篇maven项目中java与scala如何混用,今天来看看gradle项目中如何达到同样的效果: 一.目录结构 基本上跟maven一样,tips:这一堆目录结构不用死记,后面会讲如何用gra ...

  3. vue2.0 开发实践总结之入门篇

    vue2.0 据说也出了很久了,博主终于操了一次实刀. 整体项目采用  vue +  vue-router +  vuex (传说中的vue 全家桶 ),构建工具使用尤大大推出的vue-cli 后续文 ...

  4. python基础-牛逼的三层循环,实现想在那里退出,就在那里退出。

    #!/usr/bin/env python # -*- coding:utf-8 -*- #Author: nulige tag=True #设置tag控制他,只要一输入Flash就退出整个循环 wh ...

  5. 软件开发学习笔记 <二>软件开发模型、Up、Rup、敏捷Up

    软件开发过程(process) 是一个将用户需求转化为软件系统所需要的活动的集合. 软件生命周期(SDLC,Software Devlopment Life Cycle) 软件从孕育.诞生.成长.成熟 ...

  6. JBPM4.4业务流程管理框架详细解读

    1. 什么是JBPM4.4业务流程管理框架? JBPM,全称是JavaBusiness Process Management(业务流程管理),它是覆盖了业务流程管理.工作流.服务协作等领域的一个开源的 ...

  7. Bioinformatics Glossary

    原文:http://homepages.ulb.ac.be/~dgonze/TEACHING/bioinfo_glossary.html Affine gap costs: A scoring sys ...

  8. ReactNative 当前url和cookies的获取

    前面大概介绍了react-native的运行helloword级别的入门,所以之后简单的东西就不写了,毕竟官网上都能够找到. reactnative官网:https://facebook.github ...

  9. RabbitMQ Lazy Queue 延迟加载

    Lazy Queue 在著名的单例设计模式中就有懒汉式的实现方式,也就是只有在你需要的时候我才去加载. 这让博主想到了以前上学的时候,每到了假期的假期作业,在假期的时候是从来不做的.只有在快开学老师要 ...

  10. MySQL字符串连接函数

    一.CONCAT(str1,str2,…) 返回结果为连接参数产生的字符串.如有任何一个参数为NULL ,则返回值为 NULL. select concat(s_id, "--", ...