LeetCode(71) Simplify Path
2015-07-27 11:01
537 查看
有解释的代码如下,参考水中的鱼:
[code]class Solution { public: string simplifyPath(string path) { int length = path.size(); //处理长度为1的特例 if(1 == length) return path; //预处理算法设计技术:将不符合要求的输入转换为符合要求的输入, //要求最后的一个字符一定为'/'以方便下面的处理,这种技巧很常用 if(path[length - 1] != '/') { path = path + "/"; length++; } stack<string> stackString; //每次提取出相邻'/'与'/'之间的字符串,如果为“//”,则提取出空字符串 int index1, index2; index1 = 0; index2 = 1; while(index2 < length) { string tmpString; while(path[index2] != '/') { tmpString.push_back(path[index2]); index2++; } if(".." == tmpString) { if(stackString.size() != 0) stackString.pop(); }else { if(tmpString == "." || tmpString == "") { //什么都不做 }else { stackString.push(tmpString); } } index1 = index2; index2++; } if(0 == stackString.size()) return "/"; string result; while(stackString.size() != 0) { result = "/" + stackString.top() + result; stackString.pop(); } return result; } };
相关文章推荐
- 【Tomcat】Tomcat报错追踪
- 时间轴two
- javascript写了个循环,但是不循环
- Git使fork项目与源项目保持一致方法
- Iterator迭代器、Comparable、Comparator比较器
- 判断手机号和邮箱
- Activity切换淡入淡出效果
- 如何用报表解决销售工作衡量问题
- CodeForces 463C - Gargari and Bishops
- android电池驱动(四)(自用)
- ASP.NET MVC的Razor引擎一:View编译原理
- vim和gvim基本配置
- Android使用http协议的GET和POST方法!!!
- SAP的实施公司有哪些?
- NSDateFormat
- 如何用报表解决销售工作衡量问题
- BAT文件语法和技巧文档
- JAVA调用CMD命令
- 修改Linux内核参数提高Nginx服务器并发性能
- oracle中的exists 和not exists 用法详解