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. 第3章—高级装配—条件化的Bean

    条件化的Bean 通过活动的profile,我们可以获得不同的Bean.Spring 4提供了一个更通用的基于条件的Bean的创建方式,即使用@Conditional注解. @Conditional根 ...

  2. 解决UnicodeDecodeError: 'ascii' code can't decode byte 0xef in position

    今天在使用python的pip安装的时候出现了这个错误 UnicodeDecodeError: 'ascii' code can't decode byte 0xef in position 7: o ...

  3. Struts2 resulttype

    本文章只介绍较为常用的三种result type 1.dispatcher 2.redirect 3.redirectAction 一.dispatcher 用于转向JSP页面,这个是默认的结果类型, ...

  4. vlog常用参数解析

    1. -f <filelist> : compile all files in filelist --------------------------------------------- ...

  5. 相片Exif协议

    今天看他们安卓在做项目遇到一个要让旋转拍摄的相片竖屏方向显示 ,网上搜了下找到了安卓的一个博客,看了下想着既然安卓有ios也应该会有,果然不出所料,确实是有.其实他们都是遵循Exif协议,百度百科也有 ...

  6. laravel5.4学习--laravel基本路由

    最基本的 Laravel 路由只接收一个 URI 和一个闭包,并以此提供一个非常简单且优雅的定义路由方法: Route::get('foo', function () {return 'Hello W ...

  7. (JSP)关于手机端(尤其是苹果手机)pdf文件无法打开的解决方案

    流的方式下载附件 <!-- @author :daisy @date : 2011-12-04 @note : 从数据库中读取BLOB图片显示 --> <%@page import= ...

  8. hdu2044 一只小蜜蜂

    和之前的楼梯题一样,递推求解 但是要注意这里可以到50,结果已经超出了Int的范围,所以要用64位保存 #include<iostream> #include<cmath> # ...

  9. spss C# 二次开发 学习笔记(三)——Spss .Net 开发

    Spss .Net 二次开发的学习过程暂停了一段时间,今天开始重启. 之前脑残的不得了,本想从网上下载一个Spss的安装包,然后安装学习.于是百度搜索Spss,在百度搜索框的列表中看到Spss17.S ...

  10. k:特殊的线性表—队列

    队列的概念:  队列是另一种特殊的线性表,它的特殊性体现在其只允许在线性表的一端插入数据元素,在线性表的另一端删除数据元素(一般会采用在线性表的表尾那端(没被head指针所指的那端)插入数据元素,在线 ...