<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實(shí)戰(zhàn)588:設(shè)計(jì)內(nèi)存文件系統(tǒng)

          共 4031字,需瀏覽 9分鐘

           ·

          2022-04-26 07:30

          算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎(chǔ)知識(shí)和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個(gè)公眾號(hào)后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

          今天和大家聊的問題叫做?設(shè)計(jì)內(nèi)存文件系統(tǒng),我們先來看題面:
          https://leetcode-cn.com/problems/design-in-memory-file-system/


          Design an in-memory file system to simulate the following functions:
          ls: Given a path in string format. If it is a file path, return a list that only contains this file's name. If it is a directory path, return the list of file and directory names?in this directory. Your output (file and directory names together) should in?lexicographic order.
          mkdir: Given a?directory path?that does not exist, you should make a new directory according to the path. If the middle directories in the path don't exist either, you should create them as well. This function has void return type.
          addContentToFile: Given a?file path?and?file content?in string format. If the file doesn't exist, you need to create that file containing given content. If the file already exists, you need to?append?given content to original content. This function has void return type.
          readContentFromFile: Given a?file path, return its?content?in string format.

          設(shè)計(jì)一個(gè)內(nèi)存文件系統(tǒng),模擬以下功能:

          ls:以字符串的格式輸入一個(gè)路徑。如果它是一個(gè)文件的路徑,那么函數(shù)返回一個(gè)列表,僅包含這個(gè)文件的名字。如果它是一個(gè)文件夾的的路徑,那么返回該 文件夾內(nèi) 的所有文件和子文件夾的名字。你的返回結(jié)果(包括文件和子文件夾)應(yīng)該按字典序排列。

          mkdir:輸入一個(gè)當(dāng)前不存在的 文件夾路徑 ,你需要根據(jù)路徑名創(chuàng)建一個(gè)新的文件夾。如果有上層文件夾路徑不存在,那么你也應(yīng)該將它們?nèi)縿?chuàng)建。這個(gè)函數(shù)的返回類型為 void 。

          addContentToFile:輸入字符串形式的 文件路徑 和 文件內(nèi)容 。如果文件不存在,你需要?jiǎng)?chuàng)建包含給定文件內(nèi)容的文件。如果文件已經(jīng)存在,那么你需要將給定的文件內(nèi)容 追加 在原本內(nèi)容的后面。這個(gè)函數(shù)的返回類型為 void 。

          readContentFromFile:輸入 文件路徑 ,以字符串形式返回該文件的 內(nèi)容 。

          示例

          輸入:
          ["FileSystem","ls","mkdir","addContentToFile","ls","readContentFromFile"]
          [[],["/"],["/a/b/c"],["/a/b/c/d","hello"],["/"],["/a/b/c/d"]]

          輸出:
          [null,[],null,null,["a"],"hello"]


          解題

          https://www.cnblogs.com/grandyang/p/6944331.html


          這道題讓我們?cè)O(shè)計(jì)一個(gè)內(nèi)存文件系統(tǒng),實(shí)現(xiàn)顯示當(dāng)前文件,創(chuàng)建文件,添加內(nèi)容到文件,讀取文件內(nèi)容等功能,感覺像是模擬一個(gè)terminal的一些命令。這道題比較tricky的地方是ls這個(gè)命令,題目中的例子其實(shí)不能很好的展示出ls的要求,其對(duì)文件和文件夾的處理方式是不同的。由于這里面的文件沒有后綴,所以最后一個(gè)字符串有可能是文件,也有可能是文件夾。比如a/b/c,那么最后的c有可能是文件夾,也有可能好是文件,如果c是文件夾的話,ls命令要輸出文件夾c中的所有文件和文件夾,而當(dāng)c是文件的話,只需要輸出文件c即可。另外需要注意的是在創(chuàng)建文件夾的時(shí)候,路徑上沒有的文件夾都要?jiǎng)?chuàng)建出來,還有就是在給文件添加內(nèi)容時(shí),路徑中沒有的文件夾都要?jiǎng)?chuàng)建出來。論壇上這道題的高票解法都新建了一個(gè)自定義類,但是博主一般不喜歡用自定義類來解題,而且感覺那些使用了自定義類的解法并沒有更簡(jiǎn)潔易懂,所以這里博主就不創(chuàng)建自定義類了,而是使用兩個(gè)哈希表來做,其中dirs建立了路徑和其對(duì)應(yīng)的包含所有文件和文件夾的集合之間的映射,files建立了文件的路徑跟其內(nèi)容之間的映射。

          最開始時(shí)將根目錄"/"放入dirs中,然后看ls的實(shí)現(xiàn)方法,如果該路徑存在于files中,說明最后一個(gè)字符串是文件,那么我們將文件名取出來返回即可,如果不存在,說明最后一個(gè)字符串是文件夾,那么我們到dirs中取出該文件夾內(nèi)所有的東西返回即可。再來看mkdir函數(shù),我們的處理方法就是根據(jù)"/"來分隔分隔字符串,如果是Java,那么直接用String自帶的split函數(shù)就好了,但是C++沒有Java那么多自帶函數(shù),所以只能借助字符串流類來處理,處理方法就是將每一層的路徑分離出來,然后將該層的文件或者文件夾加入對(duì)應(yīng)的集合中,注意的地方就是處理根目錄時(shí),要先加上"/",其他情況都是后加。下面來看addContentToFile函數(shù),首先分離出路徑和文件名,如果路徑為空,說明是根目錄,需要加上"/",然后看這個(gè)路徑是否已經(jīng)在dirs中存在,如果不存在,調(diào)用mkdir來創(chuàng)建該路徑,然后把文件加入該路徑對(duì)應(yīng)的集合中,再把內(nèi)容加入該文件路徑的映射中。最后的讀取文件內(nèi)容就相當(dāng)簡(jiǎn)單了,直接在files中返回即可,參見代碼如下:


          class?FileSystem?{
          public:
          ????FileSystem() {
          ????????dirs["/"];
          ????}
          ????
          ????vector<string> ls(string?path) {
          ????????if?(files.count(path)) {
          ????????????int?idx = path.find_last_of('/');
          ????????????return?{path.substr(idx + 1)};
          ????????}
          ????????auto?t = dirs[path];
          ????????return?vector<string>(t.begin(), t.end());
          ????}
          ????
          ????void?mkdir(string?path)?{
          ????????istringstream?is(path);
          ????????string?t = "", dir = "";
          ????????while?(getline(is, t, '/')) {
          ????????????if?(t.empty()) continue;
          ????????????if?(dir.empty()) dir += "/";
          ????????????dirs[dir].insert(t);
          ????????????if?(dir.size() > 1) dir += "/";
          ????????????dir += t;
          ????????}
          ????}
          ????
          ????void?addContentToFile(string?filePath, string?content)?{
          ????????int?idx = filePath.find_last_of('/');
          ????????string?dir = filePath.substr(0, idx);
          ????????string?file = filePath.substr(idx + 1);
          ????????if?(dir.empty()) dir = "/";
          ????????if?(!dirs.count(dir)) mkdir(dir);
          ????????dirs[dir].insert(file);
          ????????files[filePath].append(content);
          ????}
          ????
          ????string?readContentFromFile(string?filePath)?{
          ????????return?files[filePath];
          ????}
          ????
          private:
          ????unordered_map<string, set<string>> dirs;
          ????unordered_map<string, string> files;
          };


          好了,今天的文章就到這里,如果覺得有所收獲,請(qǐng)順手點(diǎn)個(gè)在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動(dòng)力 。

          上期推文:

          LeetCode1-580題匯總,希望對(duì)你有點(diǎn)幫助!
          LeetCode刷題實(shí)戰(zhàn)581:最短無序連續(xù)子數(shù)組
          LeetCode刷題實(shí)戰(zhàn)582:殺掉進(jìn)程
          LeetCode刷題實(shí)戰(zhàn)583:兩個(gè)字符串的刪除操作
          LeetCode刷題實(shí)戰(zhàn)584:尋找用戶推薦人
          LeetCode刷題實(shí)戰(zhàn)585:2016年的投資
          LeetCode刷題實(shí)戰(zhàn)586:訂單最多的客戶
          LeetCode刷題實(shí)戰(zhàn)587:安裝柵欄

          瀏覽 86
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  夜夜躁日日躁狠狠躁综合网 | 日韩久久综合 | 国产乱来╳╳A片视频 | 亚洲AV无码精品色午夜红一片 | 无码一区二区四区 |