1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        ?LeetCode刷題實戰(zhàn)71:簡化路徑

        共 3055字,需瀏覽 7分鐘

         ·

        2020-10-21 08:15

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

        今天和大家聊的問題叫做?簡化路徑,我們先來看題面:

        https://leetcode-cn.com/problems/simplify-path/

        Given an absolute path for a file (Unix-style), simplify it. Or in other words, convert it to the canonical path.


        In a UNIX-style file system, a period . refers to the current directory. Furthermore, a double period .. moves the directory up a level.


        Note that the returned canonical path must always begin with a slash /, and there must be only a single slash / between two directory names. The last directory name (if it exists) must not end with a trailing /. Also, the canonical path must be the shortest string representing the absolute path.

        題意


        以 Unix 風格給出一個文件的絕對路徑,你需要簡化它?;蛘邠Q句話說,將其轉(zhuǎn)換為規(guī)范路徑。

        在 Unix 風格的文件系統(tǒng)中,一個點(.)表示當前目錄本身;此外,兩個點 (..) 表示將目錄切換到上一級(指向父目錄);兩者都可以是復雜相對路徑的組成部分。更多信息請參閱:Linux / Unix中的絕對路徑 vs 相對路徑

        請注意,返回的規(guī)范路徑必須始終以斜杠 / 開頭,并且兩個目錄名之間必須只有一個斜杠 /。最后一個目錄名(如果存在)不能以 / 結(jié)尾。此外,規(guī)范路徑必須是表示絕對路徑的最短字符串。

        樣例

        示例 1

        輸入:"/home/"
        輸出:"/home"
        解釋:注意,最后一個目錄名后面沒有斜杠。

        示例 2

        輸入:"/../"
        輸出:"/"
        解釋:從根目錄向上一級是不可行的,因為根是你可以到達的最高級。

        示例 3

        輸入:"/home//foo/"
        輸出:"/home/foo"
        解釋:在規(guī)范路徑中,多個連續(xù)斜杠需要用一個斜杠替換。

        示例 4

        輸入:"/a/./b/../../c/"
        輸出:"/c"

        示例 5

        輸入:"/a/../../b/../c//.//"
        輸出:"/c"

        示例 6

        輸入:"/a//b////c/d//././/.."
        輸出:"/a/b/c"


        解題


        通過觀察發(fā)現(xiàn)標準路徑就是不帶任何"..", ".", "http://"這類相對路徑的標識的字符串.
        ".."具有返回上層的作用, 棧的pop操作可以實現(xiàn)這個作用.
        "."具有保持當前目錄的作用, 不壓棧不彈??梢詫崿F(xiàn)這個作用.
        "/home", 將home壓入棧中還是/home壓入棧中? 可以考慮將"/"作為分界點, 只將home壓入棧中.

        代碼框架

        1、初始化空棧
        2、所有路徑以"/"開始,
        3、所以遍歷path時, 從下標1開始
        4、打針i,j遍歷path, 從下標1開始, 對于每個字符

        4.1 如果是連續(xù)的"/", i++ , j=i;

        4.2 如果指針j指向非"/", 持續(xù)右移j

        4.3 j將停留的位置要么是path遍歷結(jié)束, 要么是"/"

        4.3.1 如果j將path遍歷結(jié)束, 可以直接substring(i,j)

        4.3.2 如果j停留在"/"處, 也可以直接substring(i,j)

        4.4 比較substring(i,j) == "..", 棧彈出,相當于回到上一層目錄

        4.5 比較substring(i,j) == ".", 讓i指向j指向的"/", 否則, 表示substring(i,j)是一個合法目錄,將其壓棧

        5、邊界處理

        5.1 所有路徑以"/"開始, 因此i,j指針下標從1開始

        5.2 遇到".."時, 需要保證棧不為空

        5.3 遇到非".", ".."時, 才需要壓棧

        5.4 所有情況都需要將i更新為j, 讓i始終指向分界字符 "/"


        class?Solution?{
        ????public?String simplifyPath(String path) {
        ????????if(path == null?|| path.length() < 1) return?path;

        ????????Stack stk = new?Stack();
        ????????// 主要是邊界問題
        ????????for(int?i = 1, j = 1; i < path.length(); i++, j = i){
        ????????????if(path.charAt(i-1) == '/'?&& path.charAt(i) == '/') { continue;}
        ????????????while(j < path.length() && path.charAt(j) != '/') j++;

        ????????????String tmp = path.substring(i, j);
        ????????????if("..".equals(tmp) && stk.size() > 0){
        ????????????????stk.pop();
        ????????????}else?if(!".".equals(tmp) && !"..".equals(tmp)){
        ????????????????stk.push(tmp);
        ????????????}
        ????????????i = j;
        ????????}

        ????????if(stk.size() == 0) return?"/";

        ????????StringBuilder sb = new?StringBuilder();
        ????????while(stk.size() > 0){
        ????????????sb.insert(0, "/"+stk.pop());
        ????????}
        ????????return?sb.toString();
        ????}
        }


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


        上期推文:

        LeetCode40-60題匯總,速度收藏!
        LeetCode刷題實戰(zhàn)61:旋轉(zhuǎn)鏈表
        LeetCode刷題實戰(zhàn)62:不同路徑
        LeetCode刷題實戰(zhàn)63:不同路徑 II
        LeetCode刷題實戰(zhàn)64:最小路徑和
        LeetCode刷題實戰(zhàn)66:加一
        LeetCode刷題實戰(zhàn)67:二進制求和
        LeetCode刷題實戰(zhàn)68:文本左右對齊
        LeetCode刷題實戰(zhàn)69:x 的平方根
        LeetCode刷題實戰(zhàn)70:爬樓梯

        瀏覽 24
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        評論
        圖片
        表情
        推薦
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            波多野吉衣中文字幕 | 色爱无码综合网国产极品网红 | 快点使劲对白露脸叫床 | 国产中文字幕视频在线 | 国产精品秘 A级 | 久久久久久国产视频 | 欧美在线观看黄 | 99精品久久久久久久免费看蜜月 | 国产l精品久久久久久久久久 | 成人在线毛片 |