Skip to content

Commit b2b647e

Browse files
committed
docs: 每日一题:LeetCode388
1 parent fe89918 commit b2b647e

3 files changed

Lines changed: 114 additions & 0 deletions

File tree

leetcode/pom.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>parent-modules</artifactId>
7+
<groupId>com.wdbyte</groupId>
8+
<version>1.0.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>leetcode</artifactId>
13+
14+
<properties>
15+
<maven.compiler.source>8</maven.compiler.source>
16+
<maven.compiler.target>8</maven.compiler.target>
17+
</properties>
18+
19+
</project>
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.wdbyte.leetcode;
2+
3+
import java.util.Stack;
4+
5+
/**
6+
* 388. 文件的最长绝对路径
7+
* https://leetcode-cn.com/problems/longest-absolute-file-path/
8+
*
9+
* @author niulang
10+
* @date 2022/04/20
11+
*/
12+
public class LeetCode388 {
13+
public static void main(String[] args) {
14+
LeetCode388 leetCode388 = new LeetCode388();
15+
//int path = leetCode388.lengthLongestPath2("dir\n file.txt");
16+
int path = leetCode388.lengthLongestPath2("dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext");
17+
System.out.println(path);
18+
}
19+
20+
public int lengthLongestPath(String input) {
21+
String[] array = input.split("\n");
22+
Stack<String> stack = new Stack();
23+
int lastTCount = 0;
24+
int maxSize = 0;
25+
for (String path : array) {
26+
int tCount = 0;
27+
while (path.contains("\t")) {
28+
tCount++;
29+
path = path.substring(path.indexOf("\t") + 1);
30+
}
31+
if (tCount > lastTCount) {
32+
stack.push(path);
33+
} else {
34+
for (int i = 0; i <= (lastTCount - tCount); i++) {
35+
if (!stack.isEmpty()) {
36+
stack.pop();
37+
}
38+
}
39+
stack.push(path);
40+
}
41+
lastTCount = tCount;
42+
if (path.contains(".")) {
43+
int size = 0;
44+
for (String s : stack) {
45+
size += s.length();
46+
}
47+
size = size + stack.size() - 1;
48+
if (size > maxSize) {
49+
maxSize = size;
50+
}
51+
}
52+
}
53+
return maxSize;
54+
}
55+
56+
public int lengthLongestPath2(String input) {
57+
String[] array = input.split("\n");
58+
Stack<Integer> stack = new Stack();
59+
int lastTCount = 0;
60+
int maxSize = 0;
61+
for (String path : array) {
62+
int tCount = 0;
63+
while (path.contains("\t")) {
64+
tCount++;
65+
path = path.substring(path.indexOf("\t") + 1);
66+
}
67+
if (tCount > lastTCount) {
68+
stack.push(path.length());
69+
} else {
70+
for (int i = 0; i <= (lastTCount - tCount); i++) {
71+
if (!stack.isEmpty()) {
72+
stack.pop();
73+
}
74+
}
75+
stack.push(path.length());
76+
}
77+
lastTCount = tCount;
78+
if (path.contains(".")) {
79+
int size = 0;
80+
for (Integer s : stack) {
81+
size += s;
82+
}
83+
size = size + stack.size() - 1;
84+
if (size > maxSize) {
85+
maxSize = size;
86+
}
87+
}
88+
}
89+
return maxSize;
90+
}
91+
92+
}

pom.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
<modules>
1111
<module>core-java-modules</module>
1212
<module>core-java-rate-limiter</module>
13+
<module>junit5-jupiter-starter</module>
14+
<module>apache-httpclient</module>
15+
<module>leetcode</module>
1316
</modules>
1417
<name>parent-modules</name>
1518
<description>Parent for all java modules</description>

0 commit comments

Comments
 (0)