LeetCode OJ:Simplify Path(简化路径)
Given an absolute path for a file (Unix-style), simplify it.
For example,
path = "/home/"
, => "/home"
path = "/a/./b/../../c/"
, => "/c"
简化路径,linux下面简单的路径操作而已,代码如下:
class Solution {
public:
string simplifyPath(string path) {
stack<string> tokenStk;
int sz = path.size();
for (int i = ; i < sz; ++i){
if (path[i] == '/') continue;
else{
string tmp = "";
for (int j = i; j < sz && path[j] != '/'; ++j, ++i){
tmp.append(, path[i]);
}
if (tmp == ".."){
if (!tokenStk.empty())tokenStk.pop();
}
else if (tmp == ".")
continue;
else
tokenStk.push(tmp);
}
} vector<string> tokenVec;
while (!tokenStk.empty()){//存储的是反向的目录,将其输出打vector中,这样比较好
tokenVec.push_back(tokenStk.top());
tokenStk.pop();
}
string ret;
if (tokenVec.empty()) ret.append(, '/');
for (int i = tokenVec.size() - ; i >= ; --i){
ret.append(, '/');
ret.append(tokenVec[i]);
}
return ret;
}
};
PS:应为短的if不喜欢加上{}的原因,找bug找了好久,下次不能继续这个坏习惯,mark一下。
下面是java版本,不得不说java的String的api比c++的要好用太多了,可以用正则表达式分割单词真是爽啊,不用向c++那样做很多次判断了,代码如所示:
public class Solution {
public String simplifyPath(String path) {
String[] pathArr = path.split("[//]+");//首先用正则表达式将整个式子按照//分开
Stack<String> s = new Stack<String>();
for(int i = 0; i < pathArr.length; ++i){
if(pathArr[i].equals("..")){
if(!s.isEmpty())
s.pop();
}else if(pathArr[i].equals(".")){
continue;
}else{
if(!pathArr[i].equals(""))//split有可能会分割出来空白的字符,这里应该注意并且剔除
s.push(pathArr[i]);
}
}
String ret = new String("");
while(!s.isEmpty()){
ret = "/" + s.pop() + ret;
}
if(ret.length() == 0)
ret += "/";
return ret;
}
}
LeetCode OJ:Simplify Path(简化路径)的更多相关文章
- [LeetCode] 71. Simplify Path 简化路径
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- [LintCode] Simplify Path 简化路径
Given an absolute path for a file (Unix-style), simplify it. Have you met this question in a real in ...
- lintcode 中等题:Simplify Path 简化路径
题目 简化路径 给定一个文档(Unix-style)的完全路径,请进行路径简化. 样例 "/home/", => "/home" "/a/./b ...
- [LeetCode] Simplify Path 简化路径
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- 【LeetCode每天一题】Simplify Path(简化路径)
Given an absolute path for a file (Unix-style), simplify it. Or in other words, convert it to the ca ...
- 071 Simplify Path 简化路径
给定一个文档 (Unix-style) 的完全路径,请进行路径简化.例如,path = "/home/", => "/home"path = " ...
- Leetcode71. Simplify Path简化路径
给定一个文档 (Unix-style) 的完全路径,请进行路径简化. 例如, path = "/home/", => "/home" path = &qu ...
- Simplify Path(路径简化)
问题: 来源:https://leetcode.com/problems/simplify-path Given an absolute path for a file (Unix-style), s ...
- LeetCode OJ:Path Sum II(路径和II)
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- LeetCode OJ:Path Sum(路径之和)
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
随机推荐
- 008-CentOS添加环境变量
在Linux CentOS系统上安装完php和MySQL后,为了使用方便,需要将php和mysql命令加到系统命令中,如果在没有添加到环境变量之前,执行“php -v”命令查看当前php版本信息时时, ...
- HDU1081:To The Max(最大子矩阵,线性DP)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1081 自己真够垃圾的,明明做过一维的这种题,但遇到二维的这种题目,竟然不会了,我也是服了(ps:猪啊). ...
- HadoopHA简述
1 概述 在hadoop2.0之前,namenode只有一个,存在单点问题(虽然hadoop1.0有 secondarynamenode,checkpointnode,buckcupnode这些,但是 ...
- delphi 改变闪动光标
delphi 改变闪动光标 // 不同风格的光标 procedure TForm1.Edit1MouseDown(Sender: TObject; Button: TMouseButton;Shift ...
- java队列的实现
队列也可以通过数组和链表两种方式来实现. 1.链表方式实现 class Node{ Node next = null; int data; public Node(int data){this.dat ...
- hdu 1686 Oulipo kmp算法
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1686 题目: Problem Description The French author George ...
- Firebug入门指南(转)
本文转自:http://www.ruanyifeng.com/blog/2008/06/firebug_tutorial.html 作者: 阮一峰 日期: 2008年6月 8日 据说,对于网页开发人员 ...
- Entity FrameWork 配置 之连接字符串隐藏或重用
C/S项目中使用EF,默认回生成app.config文件夹,软件打包安装成功之后就回生成一个对应exe.config.里面会包含配置的一些信息. 这里介绍给大家一种隐藏连接字符串的方式. 代码如下: ...
- 介绍Web项目中用到的几款JQuery消息提示插件
第一款 noty 官方网站:https://github.com/needim/noty 第二款 artDialog artDialog是一个精巧的web对话框组件,压缩后只有十多KB,并且不依赖其他 ...
- 深入解析Koa之核心原理
这篇文章主要介绍了玩转Koa之核心原理分析,本文从封装创建应用程序函数.扩展res和req.中间件实现原理.异常处理的等这几个方面来介绍,写的十分的全面细致,具有一定的参考价值,对此有需要的朋友可以参 ...