【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- ...
随机推荐
- Asp.Net Core App 部署故障示例 2
相关阅读:Windows + IIS 环境部署Asp.Net Core App 1. HTTP Error 502.5 – Process Failure 环境 Windows Server 201 ...
- C++代码文件名标准化处理工具
工具功能:批量处理C++代码文件,将C++代码文件名中大写字母改为下划线+小写字母. 为了方便代码在不同平台下的移植,代码文件命名规范为:不使用大写字母,单词之间用下划线间隔开.为此写了这个小工具,将 ...
- [5] 柱台(Cylinder)图形的生成算法
顶点数据的生成 bool YfBuildCylinderVertices ( Yreal topRadius, Yreal bottomRadius, Yreal height, Yuint slic ...
- 遇到Elements in iteration expect to have 'v-bind:key' directives.' 这个错误
解决方式一:更改VS Code编辑器的vetur配置 错误提示: [vue-language-server] Elements in iteration expect to have 'v-bind: ...
- Android中Dialog对话框的调用及监听
Android中经常会需要在Android界面上弹出一些对话框提示用户,比如App的退出的时候都会有各种框来挽留你的心,支付宝的时候输入密码的密码框,非常常见及其实用的功能,类似于JS中的alter, ...
- 【泛型】Generic 参数化类型 类型转换
参考: http://blog.csdn.net/lonelyroamer/article/details/7864531#comments http://blog.csdn.net/lonelyro ...
- Java基础(十四):泛型
一.Java 泛型: Java 泛型(generics)是 JDK 5 中引入的一个新特性, 泛型提供了编译时类型安全检测机制,该机制允许程序员在编译时检测到非法的类型. 泛型的本质是参数化类型,也就 ...
- Java基础(二):基本数据类型和变量类型
一.java基本数据类型: 变量就是申请内存来存储值.也就是说,当创建变量的时候,需要在内存中申请空间.内存管理系统根据变量的类型为变量分配存储空间,分配的空间只能用来储存该类型数据. Java 的两 ...
- Android之AlarmManager
Android平台中,Alarm Manager Service控制着闹钟和唤醒功能.和其他系统服务一样,提供了一个辅助管理类-AlarmManager,我们只需要使用AlarmManager即可调用 ...
- zypper命令使用示例
导读 Zypper是OpenSUSE和企业版SUSE中软件包管理器ZYpp的命令行接口. 主要用于:1.管理软件包:zypper可用来安装.删除.更新和查询本地或远程的软件包.2.管理仓库:zyppe ...