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

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

Corner Cases:

    • 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".

解题:

题意为简化unix路径,即省略多余字符“/”和运算“.”、"..",返回unix的真实路径;

需要考虑的特殊情况是:

1、根目录下操作"..",还是根目录

2、连续两个“/”,可看做无任何作用

3、路径末尾的"/",要消除

思路:

每次取出两个“/”之间的字符,如果是"."或者空字符表示还在本层路径,如果是".."表示返回上一层路径,如果是其他字符,则表示下一层路径名;

用栈来保存每一层路径非常合适,但是后来变成过程中,使用vector来模拟栈操作,在最后整合最终路径时反而更方便。

代码:

 class Solution {
public:
string simplifyPath(string path) {
string res, substr;
vector<string> stk;
stringstream ss(path); while(getline(ss, substr, '/')) {
if (substr == ".." and !stk.empty())
stk.pop_back();
else if (substr != ".." && substr != "." && substr != "")
stk.push_back('/' + substr);
} for (int i = ; i < stk.size(); ++i)
res += stk[i]; return res.empty() ? "/" : res;
}
};

【Leetcode】【Medium】Simplify Path的更多相关文章

  1. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  4. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  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. 【leetcode刷题笔记】Simplify Path

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

  7. 【LeetCode每天一题】Minimum Path Sum(最短路径和)

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  8. 【leetcode刷题笔记】Path Sum

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  9. 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists

    [Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...

  10. 【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman

    [Q10] Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...

随机推荐

  1. unity代码创建草和模拟风的效果

    void Start() { Test4(); } //草 private Vector3[] grassArray = new Vector3[7]; private GameObject gras ...

  2. IOS runtime动态运行时二

    在C#.Java中有编译时多态和运行时多态,在OC中,只有运行时的多态,这与它的运行机制有关.OC中,方法的调用是通过消息的传递来进行的.在IOS runtime动态运行时一http://www.cn ...

  3. echarts实现自动轮播tooltip

    最近需要实现echarts图形中hover效果轮播(即tooltip在各个数据点上轮流显示)的功能,以下就是我学习的一个过程,只是提供思路,具体场景需要自己修改.(仅针对echarts 2.2.7及以 ...

  4. C#PrintDocument打印尺寸调整

    /// <summary> /// 打印的按钮 /// </summary> /// <param name="sender"></par ...

  5. javascript 基础知识-1

    1, stringObject.charAt(index) : 返回指定位置(index)的字符 2, RegExpObject.exec(string), 用于检索字符串(string)中正则表达式 ...

  6. SQL 之获取DATE类的年月日语句

    Select CONVERT(varchar(100), GETDATE(), 0): 05 16 2006 10:57AM Select CONVERT(varchar(100), GETDATE( ...

  7. SQL Server中的流控制语句

    begin···end 该语句定义sql代码块,通常在if和while语句中使用 declare @num int ; ; begin ; print 'hello word' end if···el ...

  8. java中接口的定义

    使用interface来定义一个接口.接口定义同类的定义类似,也是分为接口的声明和接口体,其中接口体由常量定义和方法定义两部分组成.定义接口的基本格式如下: [修饰符] interface 接口名 [ ...

  9. Dubbo 体验(一)

    最近新加入一个项目组,所使用的是Dubbo,采用的架构是分布式架构,数据库采用MySQL分片.之前也接触过一下,但为了能更好融入团队,所以找Dubbo官网看文档. 才发现Dubbo的官网已搬去apac ...

  10. wampserver 更改www目录

    现在大家基本上开发php的有很大一部分都在用Wampserver,今天来讲讲怎么更改默认的www目录, 需要修改的文件有三个 apache2的配置文件 httpd.conf 和 Wampserver的 ...