-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #628 from 0xff-dev/388
Add solution and test-cases for problem 388
- Loading branch information
Showing
6 changed files
with
150 additions
and
22 deletions.
There are no files selected for viewing
61 changes: 48 additions & 13 deletions
61
leetcode/301-400/0388.Longest-Absolute-File-Path/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 91 additions & 2 deletions
93
leetcode/301-400/0388.Longest-Absolute-File-Path/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,94 @@ | ||
package Solution | ||
|
||
func Solution(x bool) bool { | ||
return x | ||
import "strings" | ||
|
||
func Solution(input string) int { | ||
|
||
ans := 0 | ||
if !strings.Contains(input, "\n\t") { | ||
if !strings.Contains(input, ".") { | ||
return ans | ||
} | ||
|
||
size := 0 | ||
for i := 0; i < len(input); i++ { | ||
if input[i] == '\n' { | ||
if size > ans { | ||
ans = size | ||
} | ||
size = 0 | ||
continue | ||
} | ||
size++ | ||
} | ||
if size > ans { | ||
return size | ||
} | ||
return ans | ||
} | ||
|
||
var dfs func(string, string, *int, int) int | ||
dfs = func(input, path string, index *int, tabs int) int { | ||
if *index >= len(input) { | ||
return tabs | ||
} | ||
|
||
l := len(input) | ||
for { | ||
// "a\n\tb.txt\na2\n\tb2.txt" | ||
i := *index | ||
// 读取一层dir | ||
tab := 0 | ||
for ; i < len(input) && input[i] == '\t'; i++ { | ||
tab++ | ||
} | ||
if tab != tabs+1 { | ||
return tab | ||
} | ||
|
||
size := 0 | ||
start := i | ||
isEnd := false | ||
for ; i < l && input[i] != '\n'; i++ { | ||
size++ | ||
if input[i] == '.' { | ||
isEnd = true | ||
} | ||
} | ||
addPath := path + "/" + input[start:i] | ||
*index = i + 1 | ||
|
||
if isEnd { | ||
if r := len(addPath); r > ans { | ||
ans = r | ||
} | ||
continue | ||
} | ||
needTabs := dfs(input, addPath, index, tab) | ||
if needTabs != tabs+1 { | ||
return needTabs | ||
} | ||
} | ||
} | ||
|
||
start, end := 0, 0 | ||
dirs := make([]string, 0) | ||
for ; end < len(input); end++ { | ||
if input[end] == '\n' { | ||
if end < len(input)-1 && input[end+1] != '\t' { | ||
dirs = append(dirs, input[start:end]) | ||
start = end + 1 | ||
} | ||
} | ||
} | ||
dirs = append(dirs, input[start:end]) | ||
for _, baseInput := range dirs { | ||
index := 0 | ||
for ; index < len(baseInput) && baseInput[index] != '\n'; index++ { | ||
} | ||
path := baseInput[:index] | ||
index++ | ||
dfs(baseInput, path, &index, 0) | ||
} | ||
return ans | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.