Given an absolute path for a file (Unix-style), simplify it.

For example,
path = "/home/", => "/home"
path = "/a/./b/../../c/", => "/c"

简化路径,linux下面简单的路径操作而已,代码如下:

  1. class Solution {
  2. public:
  3. string simplifyPath(string path) {
  4. stack<string> tokenStk;
  5. int sz = path.size();
  6. for (int i = ; i < sz; ++i){
  7. if (path[i] == '/') continue;
  8. else{
  9. string tmp = "";
  10. for (int j = i; j < sz && path[j] != '/'; ++j, ++i){
  11. tmp.append(, path[i]);
  12. }
  13. if (tmp == ".."){
  14. if (!tokenStk.empty())tokenStk.pop();
  15. }
  16. else if (tmp == ".")
  17. continue;
  18. else
  19. tokenStk.push(tmp);
  20. }
  21. }
  22.  
  23. vector<string> tokenVec;
  24. while (!tokenStk.empty()){//存储的是反向的目录,将其输出打vector中,这样比较好
  25. tokenVec.push_back(tokenStk.top());
  26. tokenStk.pop();
  27. }
  28. string ret;
  29. if (tokenVec.empty()) ret.append(, '/');
  30. for (int i = tokenVec.size() - ; i >= ; --i){
  31. ret.append(, '/');
  32. ret.append(tokenVec[i]);
  33. }
  34. return ret;
  35. }
  36. };

PS:应为短的if不喜欢加上{}的原因,找bug找了好久,下次不能继续这个坏习惯,mark一下。

下面是java版本,不得不说java的String的api比c++的要好用太多了,可以用正则表达式分割单词真是爽啊,不用向c++那样做很多次判断了,代码如所示:

  1. public class Solution {
  2. public String simplifyPath(String path) {
  3. String[] pathArr = path.split("[//]+");//首先用正则表达式将整个式子按照//分开
  4. Stack<String> s = new Stack<String>();
  5. for(int i = 0; i < pathArr.length; ++i){
  6. if(pathArr[i].equals("..")){
  7. if(!s.isEmpty())
  8. s.pop();
  9. }else if(pathArr[i].equals(".")){
  10. continue;
  11. }else{
  12. if(!pathArr[i].equals(""))//split有可能会分割出来空白的字符,这里应该注意并且剔除
  13. s.push(pathArr[i]);
  14. }
  15. }
  16. String ret = new String("");
  17. while(!s.isEmpty()){
  18. ret = "/" + s.pop() + ret;
  19. }
  20. if(ret.length() == 0)
  21. ret += "/";
  22. return ret;
  23. }
  24. }

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. oracle 禁用索引

    同步数据的时候 有索引会比较慢 可以暂时禁用索引 --禁用索引 ALTER INDEX PK_T_AUTH_USERROLE_ID UNUSABLE; --恢复索引ALTER INDEX UK_T_A ...

  2. 003-主流区块链技术特点及Hyperledger Fabric V1.0版本特点

    一.Hyperledger fabric V1.0 架构 1.逻辑架构: 2.区块链网络 3.运行时架构 二.架构总结 1.架构要点 分拆Peer的功能,将Blockchain的数据维护和共识服务进行 ...

  3. Maven学习笔记—私服(包含maven的setting.xml配置)

    为什么要用远程仓库(私服) 如果没有私服,我们所需的所有构件都需要通过maven的中央仓库和第三方的maven仓库下载到本地,而一个团队中的所有人都重复的从maven仓库下载构件,这样就加大了中央仓库 ...

  4. ThinkPHP框架基础知识三

    一.JS文件与Css文件存放位置 其实JS与Css文件放在任意位置都可以找到,只要路径正确就行. 在TP框架中我们访问的所有文件都要走入口文件index.php,相当于访问的是index.php页面. ...

  5. web标准的理解

    首先,什么是web标准?web标准是w3c组织为解决跨浏览器兼容问题而推出的关于网页开发时应遵守的规范.在网页的四个部分中网页的内容是由网页开发者自己定义的,因此这一部分无法标准化,而网页的结构(HT ...

  6. C# 字符串中正则表达式的应用

    1.截取字符串中指定内容 {"weatherinfo":{"city":"北京","cityid":"1010 ...

  7. const修饰的常量 不能被直接修改 但是可以通过指针进行间接修改

    大家都知道如下代码中,被const限定的a是不可以被直接修改的 void main() { const int a = 3; a=1; } 在C++中const修饰的常量,不能被直接修改,但是可以通过 ...

  8. JavaWeb HTTP

    1. Http协议 1.1. 什么是Http协议 Http的全称为HyperText Transfer Protocol,译为超文本传输协议,是一种详细规定了浏览器和万维网服务器之间互相通信的规则,通 ...

  9. Spark 实现自定义对象sequenceFile方式存储,读写示例(scala编写)

    package com.fuge.bigdata.datahub.analysis import java.io.{DataInput, DataOutput} import com.fuge.big ...

  10. Spring_泛型依赖注入