加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

[leetcode] simplify-path

发布时间:2020-12-15 23:26:53 所属栏目:安全 来源:网络整理
导读:时间限制: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
时间限制: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,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 };

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读