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".
class Solution {
public:
string simplifyPath(string path) {
vector<string> str;
stringstream ss(path);
string sub;
while(getline(ss,sub,'/'))
{
if(sub=="."||sub=="")
continue;
if(sub==".."&&str.size())
str.pop_back();
if(sub!="..")
str.push_back(sub);
}
string res;
for(string s:str)
res+="/"+s;
if(res.empty())
res+="/";
return res;
}
};
simplify-path-字符串处理,去除多余路径的更多相关文章
- 字符串中去除多余的空格保留一个(MS SQL Server)
大约2年前,写过一篇<字符串中去除多余的空格保留一个(C#)>https://www.cnblogs.com/insus/p/7954151.html 今天,Insus.NET使用MS S ...
- 字符串中去除多余的空格保留一个(C#)
在C#的字符串,其中有许多空格,现要求是把多余的空格去除保留一个.原理是使用Split()方法进行分割,分割有一个选项是RemoveEmptyEntries,然后再把分割后的字符串Join起来. st ...
- 71. Simplify Path压缩文件的绝对路径
[抄题]: Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/&q ...
- 生成一个uuid字符串,并去除多余的符号
for(int i=0;i<10;i++){ String uuid = UUID.randomUUID().toString().replaceAll("-", " ...
- [LintCode] Simplify Path 简化路径
Given an absolute path for a file (Unix-style), simplify it. Have you met this question in a real in ...
- 【LeetCode】71. Simplify Path 解题报告(Python)
[LeetCode]71. Simplify Path 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 56. Edit Distance && Simplify Path
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
- leetcode面试准备:Simplify Path
leetcode面试准备:Simplify Path 1 题目 Given an absolute path for a file (Unix-style), simplify it. For exa ...
- 【LeetCode】71. Simplify Path
Simplify Path Given an absolute path for a file (Unix-style), simplify it. For example,path = " ...
随机推荐
- Android加密解密
随笔分类 - Android加密解密 Android数据加密之异或加密算法 摘要: 前言: 这几天被公司临时拉到去做Android IM即时通信协议实现,大致看了下他们定的协议,由于之前没有参与,据说 ...
- Java反编译工具CFR,Procyon简介
Java反编译工具有很多,个人觉得使用最方便的是jd-gui,当然jad也不错,jd-gui主要提供了图形界面,操作起来很方便,但是jd-gui很久没有更新了,java 7出来很久了,jd-gui在反 ...
- [转]让Nginx支持ThinkPHP的URL重写和PATHINFO
From : http://www.jzxue.com/wangzhankaifa/php/201108/08-8396.html ThinkPHP支持通过PATHINFO和URL rewrite ...
- jcseg-1.8.7版本发布 - 多配置适应+完整开发帮助文档
jcseg是使用java开发的一款开源中文分词器, 并且提供了最新版本的lucene和solr分词接口. jcseg 1.8.7版本发布了: 1. 更改了内部设计, 加入JcsegTaskConfig ...
- maven与jdk版本不一致报:Unsupported major.minor version 51.0
I recently uninstalled Java 8, to use Java 6 as I want my code/creations to be usable by more people ...
- 人脸识别中的Procruster analysis应用
本文中,我们通过Procrustes analysis来处理特征点,Procrustes analysis算法可以参考:http://en.wikipedia.org/wiki/Procrustes_ ...
- Log4cplus使用指南
1. Log4cplus简介 log4cplus是C++编写的开源的日志系统,前身是java编写的log4j系统,受Apache Software License保护,作者是Tad E. Smith ...
- 一次问题追查----短字符串签名算法引发的bug
近期开发代码, 出现了一些诡异现象.追查原因是公司使用的签名函数出现的问题. 问题: 代码使用的签名库函数, 对于<=4字节的字符串, 签名就是本身. #include<stdio.h&g ...
- C# DevExpress TabPane【转】
DevExpress TabPane能够快速简单地实现了Tab窗口的功能,相对XtraTabControl来说功能较为简单,也能够更快实现功能. 1.添加TabPane控件 将它拖到form即可. 2 ...
- Combination Sum II leetcode java
题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...