Skip to content

Commit 074b4fc

Browse files
committed
fd
1 parent a902da5 commit 074b4fc

File tree

3 files changed

+26
-72
lines changed

3 files changed

+26
-72
lines changed

solution/src/main/java/com/inuker/solution/LongestAbsoluteFilePath.java

Lines changed: 12 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.ArrayDeque;
44
import java.util.Deque;
5+
import java.util.Stack;
56

67
/**
78
* Created by dingjikerbo on 16/11/26.
@@ -11,50 +12,21 @@ public class LongestAbsoluteFilePath {
1112

1213
// 耗时4ms
1314
public int lengthLongestPath(String input) {
14-
Deque<Integer> stack = new ArrayDeque<>();
15-
int maxLen = 0;
16-
17-
String[] inputs = input.split("\n");
18-
for (String s : inputs) {
19-
int lev = s.lastIndexOf("\t") + 1; // 算出\t的个数,\t的个数代表当前目录的深度
20-
21-
// 要出栈直到当前目录的parent
22-
while (stack.size() > lev) {
15+
Stack<Integer> stack = new Stack<Integer>();
16+
int max = 0;
17+
for (String line : input.split("\n")) {
18+
int depth = line.lastIndexOf("\t") + 1;
19+
while (stack.size() > depth) {
2320
stack.pop();
2421
}
25-
26-
// parent的总长度
2722
int parentLen = stack.isEmpty() ? 0 : stack.peek();
28-
29-
// 这里要从s中去掉所有的\t,且合并parent的长度,最后再加上一个'/'
30-
int len = parentLen + s.length() - lev + 1;
31-
32-
// 给当前长度也push
33-
stack.push(len);
34-
35-
// 最后要返回的是文件的路径长度,而不是目录的长度
36-
if (s.contains(".")) {
37-
// 这里要减1因为上面后面加了个'/',而对于文件是没必要的
38-
maxLen = Math.max(maxLen, len - 1);
39-
}
40-
}
41-
42-
return maxLen;
43-
}
44-
45-
// 下面这种写法简洁一些,其实栈是可以去掉的
46-
public int lengthLongestPath2(String input) {
47-
String[] paths = input.split("\n");
48-
int[] stack = new int[paths.length + 1];
49-
int maxLen = 0;
50-
for (String s : paths) {
51-
int lev = s.lastIndexOf("\t") + 1;
52-
stack[lev + 1] = stack[lev] + s.length() - lev + 1;
53-
54-
if (s.contains(".")) {
55-
maxLen = Math.max(maxLen, stack[lev + 1] - 1);
23+
int len = parentLen + line.length() - depth;
24+
if (line.contains(".")) {
25+
max = Math.max(len, max);
26+
} else {
27+
stack.push(len + 1);
5628
}
5729
}
58-
return maxLen;
30+
return max;
5931
}
6032
}

solution/src/main/java/com/inuker/solution/SentenceScreenFitting.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class SentenceScreenFitting {
1111

1212
/**
1313
* https://discuss.leetcode.com/topic/62455/21ms-18-lines-java-solution/2
14-
* 这道题思路是将这个句子按空格拼接起来,然后铺开,这里核心是start,表示每行的开头对应的铺开序列的索引
14+
* 这道题思路是将这个句子按空格拼接起来,然后铺开,这里核心是start,表示对应的循环铺开序列的索引
1515
* 注意每行的开头一定要对应某个单词的第一个字符,所以如果不是的话需要调整start
1616
* 假如单词拼接为"abc de f ",注意结尾加了空格,铺开后为"abc de f abc de f abc de f abc de f ..."
1717
* start起始为0,下一行需要start+col,如果对到了空格则需要start++,因为每行起始不能是空格,

test/src/main/java/com/inuker/test/main.java

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,40 +24,22 @@ public class main {
2424
public static void main(String[] args) {
2525
}
2626

27-
public int maxKilledEnemies(char[][] grid) {
27+
public int lengthLongestPath(String input) {
28+
Stack<Integer> stack = new Stack<Integer>();
2829
int max = 0;
29-
for (int i = 0; i < grid.length; i++) {
30-
for (int j = 0; j < grid[0].length; j++) {
31-
if (grid[i][j] == '0') {
32-
max = Math.max(max, killedEnemies(grid, i, j));
33-
}
30+
for (String line : input.split("\n")) {
31+
int depth = line.lastIndexOf("\t") + 1;
32+
while (stack.size() > depth) {
33+
stack.pop();
3434
}
35-
}
36-
return max;
37-
}
38-
39-
private int killedEnemies(char[][] grid, int i, int j) {
40-
int count = 0;
41-
for (int k = j - 1; k >= 0 && grid[i][k] != 'W'; k--) {
42-
if (grid[i][k] == 'E') {
43-
count++;
44-
}
45-
}
46-
for (int k = j + 1; k < grid[0].length && grid[i][k] != 'W'; k++) {
47-
if (grid[i][k] == 'E') {
48-
count++;
35+
int parentLen = stack.isEmpty() ? 0 : stack.peek();
36+
int len = parentLen + line.length() - depth;
37+
if (line.contains(".")) {
38+
max = Math.max(len, max);
39+
} else {
40+
stack.push(len + 1);
4941
}
5042
}
51-
for (int k = i - 1; k >= 0 && grid[k][j] != 'W'; k--) {
52-
if (grid[k][j] == 'E') {
53-
count++;
54-
}
55-
}
56-
for (int k = i + 1; k < grid.length && grid[k][j] != 'W'; k++) {
57-
if (grid[k][j] == 'E') {
58-
count++;
59-
}
60-
}
61-
return count;
43+
return max;
6244
}
6345
}

0 commit comments

Comments
 (0)