天天看點

71-簡化路徑(Simplify Path)

題目描述

中文

以 Unix 風格給出一個檔案的絕對路徑,你需要簡化它。或者換句話說,将其轉換為規範路徑。

	在 Unix 風格的檔案系統中,一個點(.)表示目前目錄本身;此外,兩個點 (..) 表示将目錄切換到上一級(指向父目錄);兩者
	都可以是複雜相對路徑的組成部分。更多資訊請參閱:Linux / Unix中的絕對路徑 vs 相對路徑
	
	請注意,傳回的規範路徑必須始終以斜杠 / 開頭,并且兩個目錄名之間必須隻有一個斜杠 /。最後一個目錄名(如果存在)不能以 / 結
	尾。此外,規範路徑必須是表示絕對路徑的最短字元串。
           

英文

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. For more information, see: Absolute path vs relative path 
	in Linux/Unix
	
	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.
           

示例1

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

示例2

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

示例3

輸入:"/home//foo/"
輸出:"/home/foo"
解釋:在規範路徑中,多個連續斜杠需要用一個斜杠替換。
           

示例4

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

示例5

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

示例6

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

思路分析

1.好吧,我現在也沒什麼思路...可不可以嘗試用暴力的方法做呢?暴力+if判斷是否可行呢?我們來試一下。
           
class Solution {
    public String simplifyPath(String path) {
        String[] temp = path.split("\\/");          //用将字元串切割
        List<String> list = new ArrayList<String>();    //存放絕對路徑目錄
        for(String s:temp){
            if(s.length() != 0){
                if(s.equals("..")){          //如果是傳回上層 且可以傳回
                    if(list.size() > 0){
                        list.remove(list.size()-1);
                    }
                    else{
                        continue;
                    }
                }
                else if(s.equals(".")){
                    continue;
                }    
                else{
                    list.add(s);
                }
            }
        }
        String res = new String();
        for(String s:list){
            res = res + "/" + s;
        }
        if(res.length() == 0)           //如果最後路徑為空,傳回根目錄
            res = "/";
        return res;
    }
}
           
  • 很明顯,是可以的,解決過程就是先将字元串以"/"切割開,然後進行邏輯判斷,最後拼接路徑就可以解決了。現在讓我們用python3再寫一次。
class Solution:
    def simplifyPath(self, path: str) -> str:
        strings = path.split("/")
        paths = []
        for string in strings:
            if len(string) != 0:
                if string == "..":
                    if len(paths) > 0:
                        del paths[len(paths) - 1]
                    else:
                        continue
                elif string == '.':
                    continue
                else:
                    paths.append(string)
        res = ""
        for path in paths:
            res = res + "/" + path
        if len(res) == 0:
            res = "/"
        return res
           
  • 但是有沒有更為聰明的方法呢?emmm,我暫時想不到,咱們來看下别人的代碼。
  • 額。好吧,好像大家思路都一樣,隻不過是用棧來做的,打擾了,今天就到這裡。