getline(ss, temp, '/')示例
2016-04-02 16:17
197 查看
Given an absolute path for a file (Unix-style), simplify it.
For example,
path =
path =
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
such as
In this case, you should ignore redundant slashes and return
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".
string simplifyPath(string path) { string res, temp; vector<string> stk; stringstream ss(path); while (getline(ss, temp, '/')) { if (temp == "." || temp == "") continue; else if (temp == ".."&&!stk.empty()) stk.pop_back(); else if(temp!="..") stk.push_back(temp); } for (auto str : stk) res += "/" + str; return res.empty() ? "/" : res; }
相关文章推荐
- 【NOIP模拟】愉快的logo设计
- 【程序人生】:一些编程语言及发展方向
- Servlet与Struts的区别
- 【千里码】老王装货 最优化问题
- 山东理工3258 Square Number(质因数分解+乱搞)
- poj 3009 curling
- Java类的生命周期
- movsx汇编指令说明
- 92. Reverse Linked List II
- Unity3D外包
- Activity的生命周期
- iOS 百度地图 设置显示区域 中心点和范围
- Android 设备唯一标识生成方式
- PyCharm 2016.1 for Mac 激活方法分享
- 读写锁总结
- SDAU课程练习2 1010
- jvm(10)-早期(编译期)优化
- 131. Palindrome Partitioning回溯算法
- 1087. All Roads Lead to Rome (30)
- CentOS7 下面安装Mysql MMM