您当前的位置: 首页 > 

对方正在debug

暂无认证

  • 4浏览

    0关注

    399博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

简化路径(stringstream+getline)

对方正在debug 发布时间:2020-02-17 22:32:37 ,浏览量:4

题目:https://leetcode-cn.com/problems/simplify-path/ 参考:https://leetcode-cn.com/problems/simplify-path/solution/cli-yong-stringstreamhe-getlinefen-ge-zi-fu-chuan-/

class Solution {
public:
    string simplifyPath(string path) {
    	/*
		*利用getline,每次以/为分隔符
		*
		*/
        stringstream is(path);
        vector strs;
        string res = "", tmp = "";
        while(getline(is, tmp, '/')) {
            if(tmp == "" || tmp == ".")
                continue;
            else if(tmp == ".." && !strs.empty())
                strs.pop_back();
            else if(tmp != "..")
                strs.push_back(tmp);
        }
        for(string str:strs) 
            res +=  "/" + str;
        if(res.empty())
            return "/";
        return res;
    }
};
关注
打赏
1664895754
查看更多评论
立即登录/注册

微信扫码登录

0.0369s