【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- ...
随机推荐
- PhpSpreadsheet生成Excel时实现单元格自动换行
PhpSpreadsheet是PHPExcel的替代版本,PHPExcel的作者已经停止了更新,今天尝试了使用PhpSpreadsheet生成Excel的时候支持单元格内的自动换行,发现用法其实差不多 ...
- ubuntu下mongodb常用命令
1. 启动脚本 #!/bin/bash mongod --dbpath /usr/local/mongodb/data1 chmod +x run-mongodb 2. 关闭数据库服务 官方文档说可以 ...
- 35个Jquery应用实例
Jquery库及相应插件如今红遍网络,收集了网络上有关JQuery的35个精彩使用例子,在此统一展示供JQuery使用时的查询. 1. 选择网页元素jQuery的基本设计和主要用法,就是" ...
- 关于C++ 中POD类型的解析
转自: http://liuqifly.spaces.live.com/blog/cns!216ae3a149106df9!221.entry (C++-98:1.8;5)给出的定义:将对象的各字节拷 ...
- 【SpringCloud】Netflix源码解析之Ribbon:负载均衡策略的定义和实现
Ribbon负载均衡策略定义 IRule其实就只做了一件事情Server choose(Object key),可以看到这个功能是在LB中定义(要求)的,LB把这个功能委托给IRule来实现.不同的I ...
- 搜狐视频Redis私有云平台CacheCloud
一.CacheCloud是做什么的 CacheCloud提供一个Redis云管理平台:实现多种类型(Redis Standalone.Redis Sentinel.Redis Cluster)自动部署 ...
- (转)[Unity3D]UI方案及制作细节(NGUI/EZGUI/原生UI系统) 内附unused-assets清除实例
转载请留下本文原始链接,谢谢.本文会不定期更新维护,最近更新于2013.09.17. http://blog.sina.com.cn/s/blog_5b6cb9500101bplv.html ...
- [Functional Programming] mapReduce over Async operations with first success prediction (fromNode, alt, mapReduce, maybeToAsync)
Let's say we are going to read some files, return the first file which pass the prediction method, t ...
- 【图解】javaScript组成结构
- vb.net 模拟UDP通信
Imports System.Net Imports System.Text.Encoding Public Class Form1 Dim publisher As New Sockets.UdpC ...