Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Solution {
- /**
- * @param input an abstract file system
- * @return return the length of the longest absolute path to file
- */
- public int lengthLongestPath(String input) {
- // Write your code here
- int result = 0;
- int[] path = new int[input.length() + 2];
- String[] st = input.split("\n");
- for (String line : st){
- String name = line.replaceAll("(\t)+", "");
- int depth = line.length() - name.length();
- if(name.contains("."))
- result = Math.max(result, path[depth] + name.length());
- else
- path[depth + 1] = path[depth] + name.length() + 1;
- }
- return result;
- }
- }
- // version: 高频题班
- public class Solution {
- /*
- * @param input an abstract file system
- * @return return the length of the longest absolute path to file
- */
- public int lengthLongestPath(String input) {
- // Write your code here
- if (input.length() == 0) {
- return 0;
- }
- int ans = 0;
- int[] level_size = new int[input.length() + 1];
- for (String line : input.split("\n")) {
- int level = line.lastIndexOf('\t') + 2;
- int len = line.length() - (level - 1);
- if (line.contains(".")) {
- ans = Math.max(ans, level_size[level - 1] + len);
- } else {
- level_size[level] = level_size[level - 1] + len + 1;
- }
- }
- return ans;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment