Leetcode Algorithm Competition title: Longest Absolute File Path
Original link: https://leetcode.com/contest/1/problems/longest-absolute-file-path/
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:dir subdir1 subdir2 file.extthe directory dir contains an empty sub-directory SubDir1 and a s Ub-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:dir subdir1 file1.ext subsubdir1 subdir2 subsubdir2 file2.extthe dir Ectory dir contains, sub-directories SubDir1 and SubDir2. SubDir1 contains a file File1.ext and an empty second-level sub-directory Subsubdir1. SubDir2 contains a second-level sub-directory subsubdir2 containing a file file2.ext.We is interested in finding the long EST (number of characters) absolute path to a file within our file system. For example, in the second example above, the longest absolute path was "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 would not contain a. Time complexity Required:o (n) where n is the size of the input string. Notice that a/aa/aaa/file1.txt are not the longest file path, if there is another path aaaaaaaaaaaaaaaaaaaaa/sth.png.
Picture format:
Java Algorithm implementation
PackageCode;Importjava.util.ArrayList; Public classSolution {Static intStartpos=0; Static intNextlevel=0; Public Static voidMain (String args[])throwsexception{intLen=lengthlongestpath ("A\n\taa\n\t\tfile1.txt\naaaaaaaaaaaaaaaaaaaaa\n\tsth.png"); System.out.println (len); } Public Static intLengthlongestpath (String input) {startpos=0; MyList List=NewMyList (NULL, 0, 0,NULL);//creates a new empty mylist that represents the root directoryDeal (List,input, 0); if(list.containsfile) {returnList.sublength-1; } Else{ return0; } } Public Static voidDeal (MyList parent,string input,intcurlevel) { while(Startpos<input.length ()) {//Startpos is a global pointer to identify which part of the string is currently being processedString curstr=input.substring (startpos); intLen=curstr.indexof (' \ n '); if(len>0) {startpos+=len+1; String FileName=curstr.substring (0, Len); MyList Current=NewMyList (Parent,len,-1,filename);//deliberately will sublength=-1Parent.subs.add (current); //current.mylength=filename.length ();Current.mylength=len;//eliminates the filename.length () function call if(Filename.indexof ('. ') >0) {Parent.containsfile=true; if(parent.sublength<len+1) {Parent.sublength=len+1; }} nextlevel=findnextlevel (input); Startpos+=Nextlevel; if(nextlevel==curlevel+1){ //It's just the next level .deal (Current,input,nextlevel); if(current.containsfile) {Parent.containsfile=true; intNewlength=current.mylength+current.sublength+1; if(parent.sublength<newlength) {Parent.sublength=newlength; } } } if(nextlevel<curlevel) { /*Note that the file or directory name hierarchy to be processed next is higher than this level, so to exit the current level of processing, it is because the next level may be higher than the previous levels, so the nextlevel should be defined as a global variable, so that the exit of a level D After EAL, the nextlevel is complete, * when you return to the previous deal environment in the stack, you will continue to compare currentlevel and nextlevel in that stack environment. */ Break; } //if nextlevel==curlevel, then continue to loop } Else{ //len<0 indicates that the name of the last file or directory is currently being processedstartpos+=curstr.length (); MyList Current=NewMyList (PARENT,LEN,-1,CURSTR);//deliberately will sublength=-1Parent.subs.add (current); if(Curstr.indexof ('. ') >0){ //is fileparent.containsfile=true; if(Parent.sublength<curstr.length () +1) {Parent.sublength=curstr.length (+1); } } Break; } } } Public Static intfindnextlevel (String input) {intLevel=0; while(Input.charat (startpos+level) = = ' \ t ') {//judging hierarchy by number of tabslevel++; } returnLevel ; } Static classmylist{; String MyName; MyList parent; intmylength; intsublength; Booleancontainsfile=false;//By default, there is no file under the directoryarraylist<mylist>subs=NewArraylist<>(); PublicMyList (MyList Parent,intMylength,intsublength,string MyName) { This. parent=parent; This. mylength=mylength; This. sublength=sublength; This. myname=MyName; } }}
Algorithm Match (1)----Longest Absolute File Path