71. Simplify Path
来源:互联网 发布:mac同步iphone照片 编辑:程序博客网 时间:2024/06/04 01:07
class Solution {public: string simplifyPath(string path) { int n=path.size(); vector<string> paths; int i,j; for(i=0;i<n;i++) { string tmp=""; while(i<n&&path[i]!='/') { tmp+=path[i]; i++; } if(tmp==".") continue; else if(tmp==""&&!paths.empty()) continue; else if(tmp=="..") { if(!paths.empty()&&!(paths.size()==1&&paths[0]=="")) paths.pop_back(); } else paths.push_back(tmp); } if(paths.size()==0||(paths.size()==1&&paths[0]=="")) return "/"; string ans=paths[0]; for(i=1;i<paths.size();i++) { ans+="/"; ans+=paths[i]; } return ans; }};
update:
class Solution {public: string simplifyPath(string path) { int n=path.size(); vector<string> paths; int i,j; for(i=0;i<n;) { while(i<n&&path[i]=='/') i++; string tmp=""; while(i<n&&path[i]!='/') { tmp+=path[i]; i++; } if(tmp==".."&&paths.size()!=0) paths.pop_back(); else if(tmp!=""&&tmp!="."&&tmp!="..") paths.push_back(tmp); } string ans=""; if(paths.size()==0) return "/"; for(i=0;i<paths.size();i++) { ans+="/"+paths[i]; } return ans; }};
0 0
- LeetCode 71. Simplify Path
- LeetCode --- 71. Simplify Path
- [Leetcode] 71. Simplify Path
- [leetcode] 71.Simplify Path
- 71.Simplify Path
- 71.Simplify Path
- 71. Simplify Path
- [leetcode] 71.Simplify Path
- 71. Simplify Path
- Leetcode 71. Simplify Path
- LeetCode 71. Simplify Path
- Leetcode 71. Simplify Path
- 71. Simplify Path LeetCode
- 71. Simplify Path
- 71. Simplify Path
- 71. Simplify Path
- 71. Simplify Path
- leetcode 71. Simplify Path
- IT项目管理知识体系九大知识领域
- 狂拽酷炫吊炸天:用 PHP 协程实现多任务协作
- LibGdx文档译读(久)Net接口
- 在线医疗平台开发实战04-AutoMapper使用及无法引用问题解决
- 关于ResponseBody注解
- 71. Simplify Path
- 猜数字
- 常用编码工具类
- java的初接触(1)
- java I/O流(3)键盘录入,读取转换流和写入转换流
- PHP生成器 (generator)和协程的实现
- 常用数据检查类
- TortoiseSVN客户端重新设置用户名和密码
- mac下phpize报错Cannot find autoconf. Please check your autoconf installation解决方法