388. 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.ext

The directory dir contains an empty sub-directory subdir1 and a sub-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.ext

The directory dir contains two 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 are interested in finding the longest (number of characters) absolute path to a file within our file system. For example, in the second example above, the longest absolute path is "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 will not contain a ..

Time complexity required: O(n) where n is the size of the input string.

Notice that a/aa/aaa/file1.txt is not the longest file path, if there is another path aaaaaaaaaaaaaaaaaaaaa/sth.png.

这个题目总体是还是一个树类型的数据结构的题目,我在这里采用二叉树的结构完成的。

truct Node{
  int Length;  
  int level;
  int tl;
  Node* p;//指向双亲结点
  Node* b;//指向下一个兄弟结点
  Node* fc;//指向第一个儿子结点
};
struct File{
  Node *n;
  File *next;
};

其中的Node用来存储整个文件系统的数据结构,包括所有的文件夹及文件。而File是一个链接所有文件的链表,用来简化查找longest file path。

具体实现代码如下:

int lengthLongestPath(string input) {
        input += ‘\"‘;
        int i=0;
        string b = "";
        Node *Tree = new Node;
        Node *node;
        Node *fnode = Tree;
        Node *cnode = Tree;
        File *file = new File;
        file->next = NULL;
        File *f = file;
        File *nf;
        int l = 1;
        char c;

Tree->p = NULL;
        Tree->fc = NULL;
        Tree->Length = -1;
        Tree->level = 0;
        Tree->b = NULL;
        while(i<input.size())
        {
            if((input.at(i)!=‘\n‘&&input.at(i)!=‘\"‘)&&input.at(i)!=‘\t‘)
            {
                c = input.at(i);
                b += c;
                ++i;
            }
            else{
                node = new Node;
                node->b = NULL;
                node->fc = NULL;
                //cout<<cnode->level;
                if(l == cnode->level + 1)
                {
                    node->level = cnode->level + 1;
                    node->p = cnode;
                    if(fnode->fc == NULL)
                    {
                        fnode->fc = node;
                    }
                }
                else{
                    node->level = l;
                    //Node *fn = fnode;
                    //cout<<fn->level;
                    fnode = cnode;
                    int k = l - fnode->level;
                    while(k != 1)
                    {
                        fnode = fnode->p;
                        k = l - fnode->level;
                    }
                    node->p = fnode;

cnode = fnode->fc;
                    if(cnode==NULL)
                    {
                        fnode->fc = node;
                    }
                    else{
                        while(cnode->b!=NULL)
                        {
                            cnode = cnode->b;
                        }
                        cnode->b = node;
                    }
                }
                node->Length = b.size() + node->p->Length + 1;
                cnode = node;

bool flag;
                flag = false;

int j=b.size()-1;
                for(;j>=0;--j)
                {
                    if(b.at(j)==‘.‘)
                    {
                        flag = true;
                        break;
                    }
                }

if(flag == true)
                {
                    //cout<<node->Length<<endl;
                    nf = new File;
                    nf->n = node;
                    nf->next = NULL;
                    f->next = nf;
                    f = f->next;
                }
                if(i==input.size()-1)
                    break;
                l=1;
                i+=1;
                while(input.at(i)==‘\t‘)
                {
                    i+=1;
                    ++l;
                }
                b = "";
                //cout<<node->Length<<endl;
            }
        }
        File* ff = file;
        int maxnum = 0;
        while(ff->next!=NULL)
        {
            if(ff->next->n->Length > maxnum)
                maxnum = ff->next->n->Length;
            ff = ff->next;
        }

return maxnum;
    }
};

时间: 2024-10-23 05:09:34

388. Longest Absolute File Path的相关文章

LeetCode 388. Longest Absolute File Path

原题链接在这里:https://leetcode.com/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.ext The dire

算法比赛(1)----Longest Absolute File Path

Leetcode算法比赛题目之:Longest Absolute File Path 原文链接: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" rep

Longest Absolute File Path -- LeetCode

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.ext The directory dir contains an empty sub-directory subdir1 and a sub-directory 

Leetcode: 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.ext The directory dir contains an empty sub-directory subdir1 and a sub-directory

ueditor百度富文本编辑器linux下报错: class path resource [config.json] cannot be resolved to absolute file path because it does not reside in the file system

具体报错信息如下 java.io.FileNotFoundException: class path resource [config.json] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/home/java/wcq-new-server/wcq/wcq-admin/target/wcq-admin.jar!/BOOT-INF/classes!

项目windows运行正常,而Linux上运行报错: class path resource [kwhRules.json] cannot be resolved to absolute file path because it does not reside in the file system

java.io.FileNotFoundException:class path resource [kwhRules.json] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/root/app/target/app.jar!/BOOT-INF/classes!/kwhRules.json 更改前代码: void initRules() throw

[LintCode] System Longest 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.ext The directory dir contains an empty sub-directory subdir1 and a sub-directory 

java.nio.file.Path

public interface Path extends Comparable<Path>, Iterable<Path>, Watchable 1. A Path represents a path that is hierarchical and composed of a sequence of directory and file name elements separated by a special separator or delimiter. 2. A root

安卓各文件存储路径汇总(Android file path)

写下来,省得以后不记得到处翻: Environment.getDataDirectory() = /data Environment.getDownloadCacheDirectory() = /cache Environment.getExternalStorageDirectory() = /mnt/sdcard Environment.getExternalStoragePublicDirectory(“test”) = /mnt/sdcard/test Environment.getRo