【Simplify Path】cpp
题目:
Given an absolute path for a file (Unix-style), simplify it.
For example,
path = "/home/"
, => "/home"
path = "/a/./b/../../c/"
, => "/c"
- 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"
.
代码:
- class Solution {
- public:
- string simplifyPath(string path) {
- string result = "";
- path.append("/"); // some input path not end with "/"
- vector<string> ret; // use vector acting as stack
- string tmp_part = "";
- ret.push_back(string(,path[]));
- for ( size_t i = ; i < path.size(); ++i ){
- if ( path[i]=='/' ){
- if ( tmp_part==".." ){
- if ( ret.size()> )
- {
- ret.erase(ret.end()-);
- ret.erase(ret.end()-);
- }
- }
- else
- {
- if ( tmp_part!="" && tmp_part!="." )
- {
- ret.push_back(tmp_part);
- ret.push_back("/");
- }
- }
- tmp_part = "";
- }
- else
- {
- tmp_part += path[i];
- }
- }
- if ( ret.size()> && ret[ret.size()-]=="/" ) ret.erase(ret.end()-);
- for ( size_t i = ; i < ret.size(); ++i ) result += ret[i];
- return result;
- }
- };
tips:
主要思想是stack堆栈的数据结构。
1. 利用"/"来作为间隔判断符号
2. tmp_part为两个"/"之间的字符串部分(需要注意输入的path可能不以"/"结尾,解决办法是人工在path后面补上一个)
3. 如果tmp_part为“..”,则出栈两个元素(前提是栈中元素数目足够)
如果tmp_part不为“.”, 且不为".",且不为"", 则入栈(注意,还要包括后面的“/”)
其他部分靠题目提供的一些corner cases来debug了。
这里用到的一个技巧是vector来代替stack;这样做的好处是返回结果时不用将stack的元素倒序组合。
===============================================
第二次过这道题,大体路子还清晰。有一个地方没有考虑周全:从for退出来时,不能默认最后一个元素直接进栈,需要判断最后一个元素是否合规。
还有,用到stack的,进栈最好检查一下是否符合规定,不符合规定,直接往外弹元素。
- class Solution {
- public:
- string simplifyPath(string path) {
- stack<string> sta;
- int begin = ;
- int len = ;
- for ( int i=; i<path.size(); ++i )
- {
- if ( path[i]!='/' ){
- len++;
- }
- else{
- if ( len!= )
- {
- string tmp = path.substr(begin,len);
- if (tmp==".")
- {}
- else if ( tmp==".." )
- {
- if (!sta.empty()) sta.pop();
- }
- else
- {
- sta.push(path.substr(begin,len));
- }
- }
- begin = i+;
- len = ;
- }
- }
- if ( begin<path.size() )
- {
- if ( len!= )
- {
- string tmp = path.substr(begin,len);
- if (tmp==".")
- {}
- else if ( tmp==".." )
- {
- if (!sta.empty()) sta.pop();
- }
- else
- {
- sta.push(path.substr(begin,len));
- }
- }
- }
- string ret = "";
- if (sta.empty()) return "/";
- while ( !sta.empty() )
- {
- ret = "/" + sta.top() + ret;
- sta.pop();
- }
- return ret;
- }
- };
【Simplify Path】cpp的更多相关文章
- hdu 4739【位运算】.cpp
题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...
- Hdu 4734 【数位DP】.cpp
题意: 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~ ...
- 【Valid Sudoku】cpp
题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- UVA P12101 【Prime Path】
题库 :UVA 题号 :12101 题目 :Prime Path link :https://www.luogu.org/problemnew/show/UVA12101
- 【Permutations II】cpp
题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...
- 【Subsets II】cpp
题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...
- 【Sort Colors】cpp
题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
- 【Sort List】cpp
题目: Sort a linked list in O(n log n) time using constant space complexity. 代码: /** * Definition for ...
- 【Symmetric Tree】cpp
题目: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). F ...
随机推荐
- html5 canvas画板
点击查看演示地址 <!DOCTYPE HTML> <html> <title>HTML5-CanvasDrawDemo</title> <meta ...
- 关于Ajax跨域
本人因工作需求,编写了一个测试页面,在页面填写完信息之后去向一个站点请求数据,然后返回结果!一开始是直接用Ajax在脚本中去访问,没有大碍(因为目标地址是本机上的一个网站),但是当站点去外部的网站时, ...
- C#语法基础和面向对象编程
1.C#语法基础 http://www.cnblogs.com/tonney/archive/2011/03/16/1986456.html 2.C#与面向对象基础 很棒的资源,简明扼要,介绍的非常清 ...
- Knockout.Js官网学习(event绑定、submit绑定)
event绑定 event绑定在DOM元素上添加指定的事件句柄以便元素被触发的时候执行定义的JavaScript 函数.大部分情况下是用在keypress,mouseover和mouseout上. 简 ...
- tomcat学习笔记2
LNMT在网站架构中的实现过程: Client --> http --> Nginx --> reverse_proxy (http) --> tomcat (http con ...
- LotusPhp入口文件解析
LotusPhp也是单入口的框架,可以根据需要开启多个应用实例 例如前台页面可以用index.php作为入口文件,后台可以用admin.php作为入口文件,多个应用实例可以共享应用配置和类库或者根本每 ...
- spark概论
一.概述 1.轻:(1)采用语言简洁的scala编写:(2)利用了hadoop和mesos的基础设施 2.快:spark的内存计算.数据本地性和传输优化.调度优化,使其在迭代机器学习,ad-hoc ...
- C#使用SQL存储过程完整流程
存储过程就是固化在SQL数据库系统内部的SQL语句,这样做的好处是可以提高执行效率.提高数据库的安全性.减少网络流量.接下来就讲解如何在数据库中建立一个存储过程. 打开SQL2055数据库,展开“数据 ...
- 小课堂week15 年终小结
年终小结 一年的最后,想和大家回顾一下今年讲过的技术和书,用一些问答,一起来提炼一下精华. Spark 为什么需要分布式计算? 计算的增长速度超过了硬件的增长,单一服务器无法负荷.多服务器带来的是复杂 ...
- mysql数据库开发规范
对规范的遵守可用二八原则,不要教条.为满足实际需求 可忽视部分规范. 1.索引规范 *目标 |--利用最小的索引成本找到需要的行记录 *原则 |--做前缀匹配 |--选择区分度高的列做前缀索引列 |- ...