[leetcode] simplify-path

Posted yfzhou

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[leetcode] simplify-path相关的知识,希望对你有一定的参考价值。

时间限制: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)

(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, ‘ ‘, 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为止(或者‘ ‘)。
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的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode 71 简化路径simplify-path(栈)

simplify-path

Leetcode Simplify Path

leetcode 71 Simplify Path

71. Simplify Path

71. Simplify Path