1. 程式人生 > >[leetcode] simplify-path

[leetcode] simplify-path

strong found string action using ops ngs pac div

時間限制:1秒 空間限制:32768K 熱度指數:4858 本題知識點: 字符串 leetcode

算法知識視頻講解

題目描述

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".

std::getline (string)

  • C++11
(1)
istream& getline (istream&  is, string& str, char delim);
istream& getline (istream&& is, string& str, char delim);
(2)
istream& getline (istream&  is, string& str);
istream& getline (istream&& is, string& str);

Get line from stream into string

1. Extracts characters from is and stores them into str until the delimitation character delim is found (or the newline character, ‘\n‘
, for (2)).
2. The extraction also stops if the end of file is reached in is or if some other error occurs during the input operation.
3. If the delimiter is found, it is extracted and discarded (i.e. it is not stored and the next input operation will begin after it).
4. Note that any content in str before the call is replaced by the newly extracted sequence.
5. Each extracted character is appended to the string as if its member push_back was called. 從流中取字符到string 1. 從is中提取字符並存儲到str中,直到分割字符delim為止(或者‘\n‘)。 2. 如果提取到is的結尾或者在input操作中有任何錯誤發生時也將會終止提取。 3. 如果遍歷到分割字符,之前遍歷到的字符將被提取並從is中丟棄(被提取的字符將不會被存儲,下次操作將會從上次提取操作結束的位置開始) 4. 註意在調用之前str中的內容將在調用開始後被提取出來的字符序列取代。 5. 每個被提取的字符會追加到str,就像調用push_back函數一樣。

題解:

自己的版本,由於不知道c++對應Java split()的函數

 1 /*
 2     思路:
 3     "/"               "/"
 4     "/../"            "/"
 5     "/home//foo/"     "/home/foo"
 6     "/home/./foo/"    "/home/foo"
 7     "/home/a/../foo/" "/home/foo"
 8     "/home/foo"       "/home/foo"
 9 */
10 #include<string>
11 #include<vector>
12 #include<sstream>
13 using namespace std;
14 class Solution {
15 public:
16     string simplifyPath(string path) {
17         vector<string> str;
18         ostringstream oss;
19         for(int i = 0; i < path.length();i++){
20             if(path[i] == / || i == path.length() - 1){
21                 if(path[i] != / && i == path.length() - 1) oss<<path[i];
22                 string temp = oss.str();
23                 if(temp == "" || temp == "."){}
24                 else if(temp == ".."){
25                     if(str.size() != 0){
26                         str.pop_back();
27                     }
28                 }else{
29                     str.push_back(temp);
30                 }
31                 oss.str("");
32             }else{
33                 oss<<path[i];
34             }
35         }
36         oss.str("");
37         for(int i=0;i<str.size();i++){
38             oss<<"/"<<str[i];
39         }
40         if(str.size() == 0){
41             return "/";
42         }
43         return oss.str();
44     }
45 };

參考版本:

 1 class Solution {
 2 public:
 3     string simplifyPath(string path)
 4     {
 5      vector<string> res;
 6     stringstream ss(path);
 7     string sub;
 8     while(getline(ss,sub,/))
 9     {
10         if(sub=="." || sub=="")
11             continue;
12         else if(sub==".." && res.size())
13             res.pop_back();
14         else if(sub != "..")
15             res.push_back(sub);
16     }
17     string result;
18     for(string temp : res)
19         result += /+temp;
20     return res.empty() ? "/" : result; 
21     }
22 };

[leetcode] simplify-path