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 = " ...
随机推荐
- WinForm IME输入法BUG完美修复
本文来自http://hi.baidu.com/wingingbob/item/a2cb3fc0fe3bd1bb0d0a7b5b <WinForm IME输入法BUG测试>里,我描述了在. ...
- endnote插入参考文献后的对齐方式和缩进空格
原文链接 https://jingyan.baidu.com/article/63acb44a3f0f6161fcc17ed0.html 1 2 3 4 5 6 7 分步阅读 1.endnote 导入 ...
- [转]php curl 设置host curl_setopt CURLOPT_HTTPHEADER 指定host
From : http://digdeeply.org/archives/10132139.html 我们在开发测试时,有时web服务器会绑定一个域名,但是因为dns是无法解析的,我们需要设置host ...
- how to fix bug in daily work
0 QE will begin test the product when system is stable. so they may log a lot of issues, and our dai ...
- Go语言之进阶篇mysql增 删 改 查
一.mysql操作基本语法 1.创建名称nulige的数据库 CREATE DATABASE nulige DEFAULT CHARSET utf8 COLLATE utf8_general_ci; ...
- javascript this详解 面向对象
在面向对象编程语言中,对于this关键字我们是非常熟悉的.比如C++.C#和Java等都提供了这个关键字 虽然在开始学习的时候觉得比较难,但只要理解了,用起来是非常方便和意义确定的.JavaS ...
- 学 Win32 汇编[34] - 宏汇编(1)
从接触 C 语言时, 我就不大喜欢宏; 但为了看懂别人的代码也不得不去了解. 宏可定义在源程序的任意位置, 但一般放在 .data 前面.有些简单的宏可以用 equ.textequ 或 = 来代替, ...
- python机器学习sklearn 岭回归(Ridge、RidgeCV)
1.介绍 Ridge 回归通过对系数的大小施加惩罚来解决 普通最小二乘法 的一些问题. 岭系数最小化的是带罚项的残差平方和, 其中,α≥0α≥0 是控制系数收缩量的复杂性参数: αα 的值越大,收缩量 ...
- 用Python开始机器学习(2:决策树分类算法)
http://blog.csdn.net/lsldd/article/details/41223147 从这一章开始进入正式的算法学习. 首先我们学习经典而有效的分类算法:决策树分类算法. 1.决策树 ...
- pdo_mysql安装不了或是安装后用不了的修复教程
目前发现wdOS及lanmp_wdcp的RPM包安装在部分系统下安装后,在安装pdo_mysql时无法安装或安装后无法使用的问题 如在安装时提示下如下 regenerate PHP parsers.c ...