【LeetCode/C++】71. 简化路径:利用stringstream和getline分割字符串

以LeetCode 71. 简化路径 为例:

class Solution {
public:
    string simplifyPath(string path) {
        if(path.size()<=1)return path;

        stringstream is(path);

        vector strs;
        string tmp;
        while(getline(is,tmp,'/'))
        {
            if(tmp.size()==0||tmp==".")continue;

            if(tmp=="..")
            {
                if(!strs.empty())strs.pop_back();
                continue;
            }

            strs.push_back(tmp);
        }

        path="";
        for(int j=0;j

此外在知乎看到一个相关C++字符串分割的

https://www.zhihu.com/question/36642771

不明觉厉,mark一下,后续再说:sregex_token_iterator

class Solution {
public:
    string simplifyPath(string path) {
        if(path.size()<=1)return path;

        regex rx("/");
        vector sub(sregex_token_iterator(path.begin(),path.end(),rx,-1),sregex_token_iterator());

        vector sk;
        for(int i =0;i

 

你可能感兴趣的:(C++,LeetCode,leetcode,c++)