leetcode-71 simplify path
来源:互联网 发布:淘宝客服话术模板 编辑:程序博客网 时间:2024/06/05 15:39
Given an absolute path for a file (Unix-style), simplify it.
For example,
path = "/home/"
, => "/home"
path = "/a/./b/../../c/"
, => "/c"
路径打印问题,将自己碰到的特殊情况记录如下
1.两个"//"打印为一个"/"
2.出现两个点"/../"需要将上一路径弹出
3.其余情况"/./"忽略不计入最终的结果中
4."."与字符串连接时需要打印 /..hidden---->/..hidden
思路:用栈记录 //之间的字符,
参考了博主 http://blog.csdn.net/makuiyu/article/details/44497901 在此感谢
附上AC代码
class Solution {public:string simplifyPath(string path){stack<string> temp;for (int i = 0; i < path.size();){//找到/之后的字符串while (i < path.size() && path[i] == '/')i++;//找到两个//之间的字符串string ss = "";while (i < path.size() && path[i] != '/'){ss = ss + path[i];i++;}if (ss == "..") //检查到ss是两个点{if (!temp.empty()){temp.pop();i++;}}else if (ss != "."&&ss != ".."&&ss!=""){temp.push(ss);}}if (temp.empty())return "/";string s="";while (!temp.empty()){s = "/" + temp.top() + s;temp.pop();}return s;}};
0 0
- LeetCode 71 Simplify Path
- LeetCode(71) Simplify Path
- [leetcode 71] Simplify Path
- leetcode || 71、Simplify Path
- LeetCode(71) Simplify Path
- leetcode 71: Simplify Path
- 【leetcode】【71】Simplify Path
- leetcode 71 Simplify Path
- LeetCode 71 Simplify Path
- Leetcode 71 Simplify Path
- leetcode-71 simplify path
- LeetCode #71 Simplify Path
- Leetcode NO.71 Simplify Path
- [leetcode-71]Simplify Path(C)
- LeetCode(71) Simplify Path
- LeetCode 71 Simplify Path (栈)
- 【LeetCode】C# 71、Simplify Path
- 【Leetcode】71 Simplify path 简化路径
- 【LeetCode】 138. Copy List with Random Pointer C语言
- 深入浅出nodejs学习笔记-前三章 简介、模块机制、异步I/O
- Swift分支语句—switch语句
- python使用代理访问网站
- QT中多个按钮响应同一个槽函数
- leetcode-71 simplify path
- 仿智能指针
- 游戏制作要处理的相关细节
- 什么是P问题、NP问题和NPC问题
- 【Spring】使用Spring发送邮件
- AngularJS路由 $state服务、路由事件、获取路由参数
- ZOJ2208-To and Fro
- TextView 加边框
- 彻底理解python中函数内赋值操作和对象的可变性