71. Simplify Path
来源:互联网 发布:南京网博java培训费用 编辑:程序博客网 时间:2024/06/05 17:22
这题自己没想出来啥好解法。。。
先设置一个list作为存储字串的空间,之后进入循环,每次找‘/’值作为尾端,将‘/’之前的字符串提取出来作为字串,字串若为“..”,则将list最后一个pop出来。若不是“.”或“..”,则将字串push进去。最后再新建一个string将list中没有加/的字串组合起来。
最后返回的时候判断res是否为空,空的话则返回/
class Solution {public: string simplifyPath(string path) { vector<string> list; for(int a=0,b;a<path.size();a=b+1){ b=path.find('/',a)==-1?path.size():path.find('/',a); string s=path.substr(a,b-a);//从a开始,b-a个,正好找到b-1停止 if(s==".."&&!list.empty()) list.pop_back(); else if(s!="."&&s!=".."&&!s.empty()) list.push_back(s);//一开始写了两个if,结果足足多了3ms,看来以后还是要多用else } string res=""; for(s : list) res=res+'/'+s;//这里很多人喜欢写string s,但是不写string好像也不会报错 return res.empty()?"/":res; }};
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
- 《metasploit渗透测试指南》学习笔记1
- django导出csv文件的编码问题
- Reverse Integer【易】
- ViewStub 源码简解
- 重设、替换ViewPager中的Fragment
- 71. Simplify Path
- 看看React-router那些基本概念
- Android 侧栏A-Z的快速滑动搜索(三)
- NOIP 2016 签到记
- codeforces 381A. Alyona and copybooks=
- ArrayList 源码分析
- 筛选平方因子
- Linux技巧篇之一
- [入门-5] 程序组织之模块和包