★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9778434.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

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:

dirsubdir1subdir2file.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:

dirsubdir1file1.extsubsubdir1subdir2subsubdir2file2.ext

The directory dir contains two sub-directories subdir1 and subdir2subdir1 contains a file file1.ext and an empty second-level sub-directory subsubdir1subdir2 contains a second-level sub-directory subsubdir2containing 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.


假设我们以下述方式将我们的文件系统抽象成一个字符串:

字符串 "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext" 表示:

dirsubdir1subdir2file.ext

目录 dir 包含一个空的子目录 subdir1 和一个包含一个文件 file.ext 的子目录 subdir2 。

字符串 "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext" 表示:

dirsubdir1file1.extsubsubdir1subdir2subsubdir2file2.ext

目录 dir 包含两个子目录 subdir1 和 subdir2。 subdir1 包含一个文件 file1.ext 和一个空的二级子目录 subsubdir1subdir2 包含一个二级子目录 subsubdir2 ,其中包含一个文件 file2.ext

我们致力于寻找我们文件系统中文件的最长 (按字符的数量统计) 绝对路径。例如,在上述的第二个例子中,最长路径为 "dir/subdir2/subsubdir2/file2.ext",其长度为 32 (不包含双引号)。

给定一个以上述格式表示文件系统的字符串,返回文件系统中文件的最长绝对路径的长度。 如果系统中没有文件,返回 0

说明:

  • 文件名至少存在一个 . 和一个扩展名。
  • 目录或者子目录的名字不能包含 .

要求时间复杂度为 O(n) ,其中 n 是输入字符串的大小。

请注意,如果存在路径 aaaaaaaaaaaaaaaaaaaaa/sth.png 的话,那么  a/aa/aaa/file1.txt 就不是一个最长的路径。


16ms

 1 class Solution {
 2     func lengthLongestPath(_ input: String) -> Int {
 3         guard input != nil || input.count != 0 else
 4         {
 5             return 0
 6         }
 7         //创建整形堆栈,存放各级文件长度
 8         var stackOfLen = Stack<Int>()
 9         var strArray = input.characters.split{$0 == "\n"}.map(String.init)
10         var longesLen:Int = 0
11         for i in 0..<strArray.count
12         {
13             let item:String = strArray[i]
14             ////计算当前文件的层级
15             var level = LastIndexOf(item)
16             //返回到上一个同级文件的位置,注意使用count(),而非count
17             //例如:示例中遇到 \tsubdir2 时返回到与它同级的 \tsubdir1 位置
18             while(stackOfLen.count() > level)
19             {
20                 stackOfLen.pop()
21             }
22             let tempLen:Int = item.count - level
23             if stackOfLen.count() == 0
24             {
25                 stackOfLen.push(tempLen)
26             }
27             else
28             {
29                 ////加1 是返回的结果带有 / 分割
30                 let num:Int = tempLen + stackOfLen.GetLastElement() + 1
31                 stackOfLen.push(num)
32             }
33             if item.contains(".")
34             {
35                 longesLen = max(longesLen,stackOfLen.GetLastElement())
36             }
37         }
38         return  longesLen
39     }
40     //获取最后一个"\t"的索引整数位置
41     func LastIndexOf(_ item:String) -> Int
42     {
43         var num:Int = 0
44         var itemReversed:ReversedCollection<String> = item.reversed()
45         for i in 0..<itemReversed.count
46         {
47             var char:Character = itemReversed[itemReversed.index(itemReversed.startIndex, offsetBy: i)]
48             if  char == "\t"
49             {
50                 //注意索引也要反转,返回的为索引位置+1
51                 num = item.count - i
52                 break
53             }
54         }
55         return num
56     }
57
58     //堆栈的泛型通用版本
59     struct Stack<Element> {
60         var items = [Element]()
61         //入栈
62         //mutating 关键字修饰方法是为了能在该方法中修改 struct 或是 enum 的变量
63         mutating func push(_ item: Element) {
64             items.append(item)
65         }
66         //出栈
67         mutating func pop() -> Element {
68             return items.removeLast()
69         }
70         //返回堆栈中的元素个数
71         mutating func count() -> Int
72         {
73             return items.count
74         }
75         //获取最后一个元素
76         mutating func GetLastElement()->Element
77         {
78             return items[items.count-1]
79         }
80     }
81 }


8ms

 1 class Solution {
 2
 3     func lengthLongestPath(_ input: String) -> Int {
 4         guard !input.isEmpty else { return 0 }
 5
 6         let lines = input.split(separator: "\n").map{ String($0) }
 7         var sum = [Int](repeating: 0, count: input.count + 1)
 8         var result = 0
 9
10         for line in lines {
11             var count = 0
12             while line.charAt(count) == "\t" {
13                 count += 1
14             }
15             let level = count + 1
16             let len = line.count - (level - 1)
17             if line.contains(".") {
18                 result = max(result, sum[level - 1] + len + level - 1)
19             } else {
20                 sum[level] = sum[level - 1] + len
21             }
22         }
23
24         return result
25     }
26 }
27
28 extension String {
29
30     func charAt(_ i: Int) -> Character {
31         let index = self.index(self.startIndex, offsetBy: i)
32         return self[index]
33     }
34 }


12ms

 1 class Solution {
 2     func lengthLongestPath(_ input: String) -> Int {
 3         var dirPathLengths = [Int]()
 4         var maxLength = 0
 5         for pathComponent in input.split(separator: "\n") {
 6             var dirLength = 0
 7             let isFile = pathComponent.contains(".")
 8             for (depth, dirLevel) in pathComponent.split(separator: "\t", omittingEmptySubsequences: false).enumerated() {
 9                 let isPathComponentFile = dirLevel.contains(".")
10                 if depth < dirPathLengths.count {
11                     if !dirLevel.isEmpty && !isFile {
12                         dirPathLengths[depth] = dirLevel.count
13                     }
14                 } else {
15                     if !dirLevel.isEmpty && !isFile {
16                         dirPathLengths.append(dirLevel.count)
17                     }
18                 }
19                 if isFile {
20                     dirLength += (isPathComponentFile ? pathComponent.count : dirPathLengths[depth])
21                 }
22             }
23             maxLength = max(maxLength, dirLength)
24         }
25         return maxLength
26     }
27 }


16ms

 1 class Solution {
 2
 3     func lengthLongestPath(_ input: String) -> Int {
 4         guard !input.isEmpty else { return 0 }
 5
 6         let lines = input.split(separator: "\n").map{ String($0) }
 7         var sum = [Int](repeating: 0, count: input.count + 1)
 8         var result = 0
 9
10         for line in lines {
11             var count = 0
12             while line.charAt(count) == "\t" {
13                 count += 1
14             }
15             let level = count + 1
16             let len = line.count - (level - 1)
17             if line.contains(".") {
18                 result = max(result, sum[level - 1] + len + level - 1)  // Plus "/"
19             } else {
20                 sum[level] = sum[level - 1] + len
21             }
22         }
23
24         return result
25     }
26 }
27
28 extension String {
29
30     func charAt(_ i: Int) -> Character {
31         let index = self.index(self.startIndex, offsetBy: i)
32         return self[index]
33     }
34 }

转载于:https://www.cnblogs.com/strengthen/p/9778434.html

[Swift]LeetCode388. 文件的最长绝对路径 | Longest Absolute File Path相关推荐

  1. 文件系统中文件的最长(字符数)绝对路径 Longest Absolute File Path

    为什么80%的码农都做不了架构师?>>>    问题: Suppose we abstract our file system by a string in the followin ...

  2. springboot打成jar后获取resources下文件失败, cannot be resolved to absolute file path because it does not resid

    读取resources下的文件quotaShow.jasper 本地开发环境能正常下载: ClassPathResource resource = new ClassPathResource(&quo ...

  3. leetcode 388. Longest Absolute File Path | 388. 文件的最长绝对路径(栈+DFS)

    题目 https://leetcode.com/problems/longest-absolute-file-path/ 题解 没有思路的时候,看 Related Topics,知道这题在考察什么了, ...

  4. LeetCode 388. 文件的最长绝对路径(不用栈,前缀和)

    1. 题目 假设我们以下述方式将我们的文件系统抽象成一个字符串: 字符串 "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext" 表示: dirsubd ...

  5. 388. 文件的最长绝对路径

    链接:388. 文件的最长绝对路径 题解: class Solution {public:vector<string> split(string src, string delimeter ...

  6. C# 获取视频文件播放时长

    以前是用xp (32位)系统,获取视频文件长度是通过调用Shell32.dll来读取文件信息得到的,最近换win7(32位)系统,编译以前的项目代码发现无法读取了,代码没有改动,拿到在xp下运行也不行 ...

  7. DOS命令导出文件夹内所有文件的名称和全路径

    DOS命令导出文件夹内所有文件的名称和全路径的方法: 1.Windows中调出CMD窗口 2.在命令行中输入:dir /s/b >>aa.txt 3.打开文本文件aa.txt,就可以看见文 ...

  8. ftp 文件夹 上传到服务器,ftp上传文件夹到服务器 远程路径

    ftp上传文件夹到服务器 远程路径 内容精选 换一换 WinSCP工具可以实现在本地与远程计算机之间安全地复制文件.与使用FTP上传代码相比,通过 WinSCP 可以直接使用服务器账户密码访问服务器, ...

  9. Python 标准库之 os (获取当前目录、读取/设置环境变量、重命名文件、运行shell命令、创建/删除/查看目录文件、判断目录/文件/存在、获取绝对路径、获取文件名、获取换行符、获取路径分隔符)

    1. os与sys模块的官方解释如下: os This module provides a portable way of using operating system dependent funct ...

最新文章

  1. Xtrabackup备份、还原、恢复Mysql操作大全
  2. 用正则表达式匹配网址URL中最后一个反斜杠/后面的内容
  3. iOS之深入解析多环境配置的实现方案
  4. 嵌套函数中的this指向的对象
  5. 【跃迁之路】【599天】程序员高效学习方法论探索系列(实验阶段356-2018.09.27)...
  6. [C++11]不允许使用auto的四个场景
  7. 如何为火狐浏览器添加附加组件?火狐浏览器附加组件管理器使用教程
  8. 2022美赛备赛资料大全
  9. Error:配置系统未能初始化
  10. 个人打造sm2258xt固态U盘全过程分享,附量产工具和教程
  11. 最大类间方差法(大津法OTSU)
  12. mpeg文件格式分析
  13. 【笔记整理】通信原理第九章复习——线性分组码
  14. android 加花工具下载,Android 代码混淆并加花
  15. Flex富文本编辑器
  16. H.264中NALU、RBSP、SODB的关系 (弄清码流结构)
  17. TCP/IP网络编程复习(上)
  18. 【PE258】A lagged Fibonacci sequence
  19. MySQL数据库存储价格金额使用的数据类型中float、double、decimal的区别
  20. JAP中@Temporal

热门文章

  1. Cesium学习笔记(五):3D 模型 (http://blog.csdn.net/umgsoil/article/details/74572877)
  2. Python中re(正则表达式)模块函数学习
  3. 在PostgreSQL命令行psql里格式化输出json字段
  4. window.open 打开新窗口被拦截的其他解决方法
  5. JS设计模式—节流模式的实际应用
  6. Spring Boot异常
  7. 自动化部署之gitlab备份和恢复
  8. 面向对象的编程学习笔记
  9. memcached的最佳实践方案(转)
  10. VIM编辑器使用技巧