【LeetCode】71. Simplify Path
Simplify Path
Given an absolute path for a file (Unix-style), simplify it.
For example,
path = "/home/"
, => "/home"
path = "/a/./b/../../c/"
, => "/c"
- 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的更多相关文章
- 【LeetCode】71. Simplify Path 解题报告(Python)
[LeetCode]71. Simplify Path 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【一天一道LeetCode】#71. Simplify Path
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】847. Shortest Path Visiting All Nodes 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/shortest ...
- 【LeetCode】64. Minimum Path Sum 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【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 ...
- 【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 ...
- 【LEETCODE】71、验证二叉树的前序序列化
简单粗暴,代码有待优化,不过自己独立完成,没有参考任何材料,还是比较满意的 package y2019.Algorithm.stack.medium; import java.util.Stack; ...
- 【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 ...
- 【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- ...
随机推荐
- 【BZOJ】【2286】【SDOI2011】消耗战
虚树+树形DP Orz ZYF……果然好神…… 建虚树先按dfn排序,再用一个单调栈来维护当前这条[链],往里加边……说实话还没弄懂- - 留个坑吧…… RE的原因:这条链往出退的时候没写top--; ...
- natapp搭建外网服务器
首先在natapp官网注册一个账号:https://natapp.cn/ 注册好后登陆网站,点击左侧菜单中的购买隧道: 点击免费隧道后进入隧道配置页面,我这里已经配置好了直接展示如下: 点击保存后点击 ...
- PS-点击选中某一个图层
需要点击选中某一图层的时候,需要勾选[自动选择]
- HDU 5411 CRB and Puzzle (2015年多校比赛第10场)
1.题目描写叙述:pid=5411">点击打开链接 2.解题思路:本题实际是是已知一张无向图.问长度小于等于m的路径一共同拥有多少条. 能够通过建立转移矩阵利用矩阵高速幂解决.当中,转 ...
- Android MVP 构架封装
上一篇我们简单实现了一个MVP的构架,下面我们来做一个简单的封装使其使用更简单方便 源码地址RxMVP分支Tag03 最终实现目录结构如下 BasePresenter 如果每一个Activity都需要 ...
- cocos lua 加密与解密 混淆 (版本号cocos3.4)
cocos luacompile cocos luacompile Overview Usage Available Arguments Samples Overview Compile the .l ...
- Python网络爬虫 - 下载图片
下载博客园的logo from urllib.request import urlretrieve from urllib.request import urlopen from bs4 import ...
- C# 编程实现串口通信
http://blog.sina.com.cn/s/blog_6c67dab30101p3vn.html ----------------------------------------------- ...
- gradle 添加jar依赖,执行grade build时出现“程序包不存在”问题
引用的第三方依赖的包都找不到了 解决办法 group'com.suneony' version'1.0.0' apply plugin:'java' repositories { mavenLocal ...
- PyQt5教程——布局管理(4)
PyQt5中的布局管理 布局管理是GUI编程中的一个重要方面.布局管理是一种如何在应用窗口上防止组件的一种方法.我们可以通过两种基础方式来管理布局.我们可以使用绝对定位和布局类. 绝对定位 程序指定了 ...