Simplify Path

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

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

click to show corner cases.

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

核心在于编写一个以'/'为分隔符的split函数

以及用进出栈来保存最简路径。

path:"/a/./b/../../c/"

split:"a",".","b","..","..","c"

stack:push(a), push(b), pop(b), pop(a), push(c) --> c

注意:string(it1, it2)的用法

string  (InputIterator first, InputIterator last);
Copies the sequence of characters in the range [first,last), in the same order.
class Solution {
public:
static bool isSlash(char c)
{
return (c == '/');
}
static bool notSlash(char c)
{
return !isSlash(c);
}
vector<string> split(string str)
{
vector<string> ret;
string::iterator it = str.begin();
while(it != str.end())
{
it = find_if(it, str.end(), notSlash);
string::iterator it2 = find_if(it, str.end(), isSlash);
if(it != str.end())
ret.push_back(string(it, it2));
it = it2;
}
return ret;
}
string simplifyPath(string path) {
vector<string> v = split(path);
stack<string> stk;
string ret = "";
for(int i = ; i < v.size(); i ++)
{
if(v[i] == ".")
;
else if(v[i] == "..")
{
if(!stk.empty())
stk.pop();
}
else
stk.push(v[i]);
}
while(!stk.empty())
{
ret = "/" + stk.top() + ret;
stk.pop();
}
if(ret == "")
return "/";
return ret;
}
};

【LeetCode】71. Simplify Path的更多相关文章

  1. 【LeetCode】71. Simplify Path 解题报告(Python)

    [LeetCode]71. Simplify Path 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  2. 【一天一道LeetCode】#71. Simplify Path

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  3. 【LeetCode】847. Shortest Path Visiting All Nodes 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/shortest ...

  4. 【LeetCode】64. Minimum Path Sum 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  5. 【LeetCode】64. Minimum Path Sum

    Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...

  6. 【LeetCode】064. Minimum Path Sum

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

  7. 【LEETCODE】71、验证二叉树的前序序列化

    简单粗暴,代码有待优化,不过自己独立完成,没有参考任何材料,还是比较满意的 package y2019.Algorithm.stack.medium; import java.util.Stack; ...

  8. 【leetcode】1129. Shortest Path with Alternating Colors

    题目如下: Consider a directed graph, with nodes labelled 0, 1, ..., n-1.  In this graph, each edge is ei ...

  9. 【leetcode】1091. Shortest Path in Binary Matrix

    题目如下: In an N by N square grid, each cell is either empty (0) or blocked (1). A clear path from top- ...

随机推荐

  1. tcp_client.c tcp_server.c

    #include <stdlib.h> #include <stdio.h> #include <errno.h> #include <string.h> ...

  2. 启明星Exchange/outlook预定会议室终端显示解决方案

    启明星会议室预定系统(Exchange2007及其以上版本,)终端调用说明 (一)技术原理 系统采用三级刷新方式,以尽可能减少对服务器的访问压力. (1) exe程序,每隔5分钟访问Exchange, ...

  3. OpenCV 脸部跟踪(2)

          前面一篇文章中提到,我们在一副脸部图像上选取76个特征点,以及这些特征点的连通性信息来描述脸部形状特征,本文中我们会把这些特征点映射到一个标准形状模型.       通常,脸部形状特征点能 ...

  4. 4 Sum leetcode java

    题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...

  5. AS .ignore插件 忽略文件

    AS自带的.ignore文件 在AS中新建项目时,默认会创建一个.ignore文件,其中默认忽略的是 *.iml .gradle /local.properties /.idea/workspace. ...

  6. Unable to find manifest signing certificate in the certificate store

    方法一:把DEF项目的属性->Signing选项->Sign the ClickOnce manifests 勾去掉,这样就可以编绎通过了: 方法二:用记事本打开 *.csproj文件 , ...

  7. Bootstrap学习js插件篇之下拉菜单

    案例 通过此插件可以为几乎所有东西添加下拉菜单,包括导航条.标签页.胶囊式按钮. 用于导航条 导航条分为四个部分.第一部分导航头,第二部分导航列,第三部分form查询表单,第四部分导航列. <n ...

  8. IOS UITableView多选删除功能

    UITbableView作为列表展示信息,除了展示的功能,有时还会用到删除,比如购物车.收藏列表等. 单行删除功能可以直接使用系统自带的删除功能,当横向轻扫cell时,右侧出现红色的删除按钮,点击删除 ...

  9. IOS Xib使用——为控制器添加Xib文件

    Xib文件是一个轻量级的用来描述局部界面的文件,它与StoryBoard类似,都是使用Interface Bulider工具进行编辑.但是StoryBoard是重量级的,它是用来描述整个软件的多个界面 ...

  10. golang struct转map

    struct转map package main import ( "fmt" "reflect" "time" ) type User st ...