Leetcode#71 Simplify Path
用栈保存化简后的路径。把原始路径根据"/"切分成若干小段,然后依次遍历
若当前小段是"..",弹栈
若当前小段是".",什么也不做
否则,入栈
代码:
string simplifyPath(string path) {
vector<string> buffer;
char *tok = NULL; tok = strtok((char *) path.c_str(), "/");
while (tok) {
cout << tok << endl;
if (strcmp(tok, "..") == ) {
if (!buffer.empty())
buffer.pop_back();
}
else if (strcmp(tok, ".") != )
buffer.push_back(string(tok));
tok = strtok(NULL, "/");
} string res;
for (auto dir : buffer)
res += "/" + dir;
return buffer.empty() ? "/" : res;
}
Leetcode#71 Simplify Path的更多相关文章
- [LeetCode] 71. Simplify Path 简化路径
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- 【LeetCode】71. Simplify Path 解题报告(Python)
[LeetCode]71. Simplify Path 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 71. Simplify Path(M)
71. Simplify Path Given an absolute path for a file (Unix-style), simplify it. For example, path = & ...
- 【LeetCode】71. Simplify Path
Simplify Path Given an absolute path for a file (Unix-style), simplify it. For example,path = " ...
- 【一天一道LeetCode】#71. Simplify Path
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【leetcode】Simplify Path
题目简述: Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/& ...
- Java for LeetCode 071 Simplify Path
Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", ...
- 71. Simplify Path
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- Leetcode 之Simplify Path @ python
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
随机推荐
- mysql事件调度器定时删除binlog
MySQL5.1.6起Mysql增加了事件调度器(Event Scheduler),可以用做定时执行某些特定任务,来取代原先只能由Linux操作系统的计划任务来执行的工作MySQL的事件调度器可以精确 ...
- wampserver php多版本5.2--5.6和apche2.2/2.4
一.准备 wampserver2.5 php5各版本 php5.2到php5.6 apache2.2和apache2.4 二.安装 先成功安装wampserver2.5,如果安装不成功,多是vc11没 ...
- CentOS7.0安装JDK1.8.0_31
1.检查一下系统中的jdk版本 $>java -version java version "1.7.0_"OpenJDK Runtime Environment (IcedT ...
- 年薪10W和100w的人差距在哪?
12年前,我直升了硕士,在家闲得慌,去一家香港的婴幼儿杂志全职实习,每天早上8点上班,下午5点下班,一个月我负责20p左右的内容,实习工资800元. 公司很小,没有办公室政治,大家都很松散,上班打打游 ...
- jquery.tmpl 用法(附上详细案例)
js的模板引擎就和服务端的差不多,都是更好更快的拼接html用于显示,我参考了文章:http://www.cnblogs.com/zhuzhiyuan/p/3510175.html tmpl常用标签 ...
- selenium+python cooking用法 (转)
selenium-webdriver(python)--cookie处理 driver.get_cookies() 获得cookie信息 add_cookie(cookie_dict) 向cooki ...
- rails bug
variable @fontAwesomeEotPath_iefix is undefined rails generate bootstrap:install如果还有错,保证在加载主提之前impor ...
- UIViewAnimationOptions swift 2
UIView.animateWithDuration(0.5, delay: 0.5, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.0, ...
- Java当中的异常
异常:中断了正常指令流的事件,是JVM虚拟机产生的对象 异常是程序运行时产生的,和编译无关 class Test{ public static void main(String args[]){ Sy ...
- 批处理判断是否存在文件,存在则运行另外一个bat文件
现在需求如下: 使用bat文件判断是否存在ktr文件,存在则运行pan.bat,执行kettle脚本. 代码如下: @echo off @title 批处理判断文件夹是否存在 cd /d F: rem ...