【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- ...
随机推荐
- C语言编程规范
C语言编程规范 6 函数与过程 6.1 函数的功能与规模设计 函数应当短而精美,而且只做一件事.不要设计多用途面面俱到的函数,多功能集于一身的函数,很可能使函数的理解.测试.维护等变得困难. 6.2 ...
- 使用navicat工具创建MySQL存储过程
使用Navicat for MySQL工具创建存储过程步骤: 1. 新建函数(选择函数标签 -> 点击新建函数): 2.输入函数的参数个数.参数名.参数类型等: 3.编写存储过程: 代码如下: ...
- 第一章 第一个spring boot程序
环境: jdk:1.8.0_73 maven:3.3.9 spring-boot:1.2.5.RELEASE(在pom.xml中指定了) 注意:关于spring-boot的支持, 最少使用jdk7(j ...
- Java实现对Mysql的图片存取操作
1.MySQL中的BLOB类型 Mysql中可以存储大文件数据,一般使用的BLOB对象.如图片,视频等等. BLOB是一个二进制大对象,可以容纳可变数量的数据.因为是二进制对象,所以与编码方式无关.有 ...
- UVA 400 (13.08.05)
Unix ls The computer company you work for is introducing a brand new computer line and is developi ...
- Android下拉刷新-SwipeRefreshLayout
现在市面上新闻类的App基本上都有下拉刷新,算是一个标配吧,网上关于下拉刷新的博客也有很多,实现方式可以使用开源的PullToRefresh,自定义ListView,或者可以直接使用LineLayOu ...
- 请教如何改善C#中socket通信机客户端程序的健壮性
我是做Socket的新手,最近做了一个Socket客户端程序,连接Server的时候,如果server存在,并且允许连接的话,程序无错,正常执行:但是如果Server不存在,或者拒绝连接,程序就会卡住 ...
- Java基础(三):修饰符、运算符、循环结构和分支结构
一.Java修饰符: Java语言提供了很多修饰符,主要分为以下两类:访问修饰符和非访问修饰符.修饰符用来定义类.方法或者变量,通常放在语句的最前端. 1.访问控制修饰符: Java中,可以使用访问控 ...
- ComboxEdit 重要属性
DisplayMember="ComboItemName" ValueMember="ComboItemCode"IsTextEditable="Tr ...
- (剑指Offer)面试题1:赋值运算符函数
题目: 如下为类型CMyString的声明,请为该类型添加赋值运算符函数. class CMyString{public: CMyString(char* pData=NULL); CMy ...