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(简化路径)的更多相关文章

  1. [LeetCode] 71. Simplify Path 简化路径

    Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...

  2. [LintCode] Simplify Path 简化路径

    Given an absolute path for a file (Unix-style), simplify it. Have you met this question in a real in ...

  3. lintcode 中等题:Simplify Path 简化路径

    题目 简化路径 给定一个文档(Unix-style)的完全路径,请进行路径简化. 样例 "/home/", => "/home" "/a/./b ...

  4. [LeetCode] Simplify Path 简化路径

    Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...

  5. 【LeetCode每天一题】Simplify Path(简化路径)

    Given an absolute path for a file (Unix-style), simplify it. Or in other words, convert it to the ca ...

  6. 071 Simplify Path 简化路径

    给定一个文档 (Unix-style) 的完全路径,请进行路径简化.例如,path = "/home/", => "/home"path = " ...

  7. Leetcode71. Simplify Path简化路径

    给定一个文档 (Unix-style) 的完全路径,请进行路径简化. 例如, path = "/home/", => "/home" path = &qu ...

  8. Simplify Path(路径简化)

    问题: 来源:https://leetcode.com/problems/simplify-path Given an absolute path for a file (Unix-style), s ...

  9. 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 ...

  10. 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 ...

随机推荐

  1. sersync的confxml.xml文件详解

    <?xml version="1.0" encoding="ISO-8859-1"?> <head version="2.5&quo ...

  2. Java集合(4):Iterator(迭代器)

    迭代器是一种设计模式,它是一个对象,它可以遍历并选择序列中的对象,而开发人员不需要了解该序列的底层结构.迭代器通常被称为“轻量级”对象,因为创建它的代价小. Java中的Iterator功能比较简单, ...

  3. PHP HTML DOM 解析器 中文手册

    简单的PHP HTML DOM 解析器 中文手册 | PHP Simple HTML DOM Parser中文手册 目录 快速入门 如何创建HTML DOM 对象? 如何查找HTML元素? 如何访问H ...

  4. 日志体系——loging

    import loggingclass log: def __init__(self): # 文件的命名 self.logname=os.path.join(os.path.abspath(os.pa ...

  5. 浏览器 Event对象 及 属性 的兼容处理

    摘自: http://blog.csdn.net/jiachunfeng/article/details/6448186 event对象 IE 中可以直接使用 event 对象,而 FF 中则不可以, ...

  6. flex graphiclar symbol的不同比例尺切换

    private var cityGraL:GraphicsLayer;//标记城市 maxScale=50000 private var siteGraL:GraphicsLayer;//标记站点 m ...

  7. Spark机器学习5·回归模型(pyspark)

    分类模型的预测目标是:类别编号 回归模型的预测目标是:实数变量 回归模型种类 线性模型 最小二乘回归模型 应用L2正则化时--岭回归(ridge regression) 应用L1正则化时--LASSO ...

  8. 优秀 H5 案例收集 vol.1(不定期更新)

    一生要历经的三种战斗http://datang.wearewer.com/ 雍正去哪儿http://news.163.com/college/special/craftsman_h5/ 比Emoji更 ...

  9. Java结对编程之挑战出题

    Java结对编程之挑战出题 需求分析 需求 对于挑战出题来说最主要的就是要产生的式子并将重复的式子去掉. 设计思路 具体的思路: 思路一: 原先我打算用集合中的元素的不重复性进行去重,这种思路的好处就 ...

  10. Spring Boot 中修改端口和上下文路径

    通过修改application.properties内容来改变访问的端口号和上下文路径(很简单!) spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.vi ...