您的位置:首页 > 其它

[Leetcode] Simplify Path

2012-12-17 02:44 316 查看
class Solution {
public:
string simplifyPath(string path) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
stack<string> stk;
string str = "";

for (int i = 0; i < path.size(); ++i)
{
if (path[i] == '/')
{
if (str == "..")
{
if (!stk.empty()) stk.pop();
}
else if (str != "." && str != "")
{
stk.push(str);
}

str = "";
}
else
{
str += path[i];
}
}

if (str == "..")
{
if (!stk.empty()) stk.pop();
}
else if (str != "." && str != "")
{
stk.push(str);
}

if (stk.empty()) return "/";

string res = "";
while (!stk.empty())
{
res = "/" + stk.top() + res;
stk.pop();
}

return res;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: