LeetCode *** 71. Simplify Path (getline的使用)

来源:互联网 发布:teamviewer linux 使用 编辑:程序博客网 时间:2024/05/22 12:34

题目:

Given an absolute path for a file (Unix-style), simplify it.

For example,
path = "/home/", => "/home"
path = "/a/./b/../../c/", => "/c"


分析:


代码:

class Solution {public:    string simplifyPath(string path) {        string res="", tmp;        vector<string> stk;        stringstream sstm(path);                while(getline(sstm,tmp,'/')){            if(tmp==""||tmp==".")continue;            if(tmp==".."&&!stk.empty())stk.pop_back();            else if(tmp!="..")stk.push_back(tmp);        }        for(auto st:stk)res+="/"+st;        return res.empty()?"/":res;    }};


0 0