leetcode_71_Simplify Path,pathsumleetcode

來源:互聯網
上載者:User

leetcode_71_Simplify Path,pathsumleetcode

麻煩各位朋友幫忙頂一下增加人氣,如有錯誤或疑問請留言糾正,謝謝

Simplify Path
Given an absolute path for a file (Unix-style), simplify it.

For example,
path = "/home/", => "/home"
path = "/a/./b/../../c/", => "/c"
click to show corner cases.

Corner Cases:
Did you consider the case where path = "/../"?
In this case, you should return "/".
Another corner case is the path might contain multiple slashes '/' together, such as "/home//foo/".
In this case, you should ignore redundant slashes and return "/home/foo".

思路解析:
面對字串題目一定要保持清醒,先分析好題目之後再開始碼代碼。
題目的要求是輸出Unix下的最簡路徑,Unix檔案的根目錄為"/","."表示目前的目錄,".."表示上級目錄。
例如:
輸入1:
/../a/b/c/./.. 
輸出1:
/a/b
類比整個過程:
1. "/" 根目錄
2. ".." 跳轉上級目錄,上級目錄為空白,所以依舊處於 "/"
3. "a" 進入子目錄a,目前處於 "/a"
4. "b" 進入子目錄b,目前處於 "/a/b"
5. "c" 進入子目錄c,目前處於 "/a/b/c"
6. "." 目前的目錄,不操作,仍處於 "/a/b/c"
7. ".." 返回上級目錄,最終為 "/a/b"

實現方法:用一個堆棧來類比路徑的行為,遇到"."不操作,遇到".."退棧,其他情況都壓入堆棧。


//vs2012測試代碼#include<iostream>#include<stack>#include<string>using namespace std;class Solution {private:    void pathToDirectories( stack<string> &directories , string &path){string name;name.clear();path = path + '/';for(int i=0; i<path.length(); i++){if( path[i]=='/' ){if( !name.empty() )    if ( name[0]=='.' && name.length()==1 )        name.clear();else if( name[0]=='.' && name.length()==2 && name[1]=='.' ){if( !directories.empty() )directories.pop();name.clear();}else {directories.push(name);name.clear();    }}elsename = name + path[i];}}void directoriesToPath( string &ans,stack<string> &directories){ans.clear();while( !directories.empty() ){ans = directories.top() + '/'+ ans ;directories.pop();}if( !ans.empty() )ans = ans.substr( 0,ans.length()-1 );//substr 方法:返回一個從指定位置開始,並具有指定長度的子字串。ans = '/' + ans;}public:    string simplifyPath(string path) {stack<string> directories;pathToDirectories( directories,path );string ans;directoriesToPath ( ans,directories );return ans;    }};int main(){string path;getline( cin , path);  //或者cin>>s;Solution lin;cout<<lin.simplifyPath(path)<<endl;return 0;}

//方法一:自測Acceptedclass Solution {private:    void pathToDirectories( stack<string> &directories , string &path){string name;name.clear();path = path + '/';for(int i=0; i<path.length(); i++){if( path[i]=='/' ){if( !name.empty() )    if ( name[0]=='.' && name.length()==1 )        name.clear();else if( name[0]=='.' && name.length()==2 && name[1]=='.' ){if( !directories.empty() )directories.pop();name.clear();}else {directories.push(name);name.clear();    }}elsename = name + path[i];}}void directoriesToPath( string &ans,stack<string> &directories){ans.clear();while( !directories.empty() ){ans = directories.top() + '/'+ ans ;directories.pop();}if( !ans.empty() )ans = ans.substr( 0,ans.length()-1 ); ////substr 方法:返回一個從指定位置開始,並具有指定長度的子字串。ans = '/' + ans;}public:    string simplifyPath(string path) {stack<string> directories;pathToDirectories( directories,path );string ans;directoriesToPath ( ans,directories );return ans;    }};


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.