题目描述

Given an absolute path for a file (Unix-style), simplify it.

For example,
path ="/home/", =>"/home"
path ="/a/./b/../../c/", =>"/c"

click to show corner cases.

Corner Cases:

  • 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".
 
记住stringstream以及getline函数的作用
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-字符串处理,去除多余路径的更多相关文章

  1. 字符串中去除多余的空格保留一个(MS SQL Server)

    大约2年前,写过一篇<字符串中去除多余的空格保留一个(C#)>https://www.cnblogs.com/insus/p/7954151.html 今天,Insus.NET使用MS S ...

  2. 字符串中去除多余的空格保留一个(C#)

    在C#的字符串,其中有许多空格,现要求是把多余的空格去除保留一个.原理是使用Split()方法进行分割,分割有一个选项是RemoveEmptyEntries,然后再把分割后的字符串Join起来. st ...

  3. 71. Simplify Path压缩文件的绝对路径

    [抄题]: Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/&q ...

  4. 生成一个uuid字符串,并去除多余的符号

    for(int i=0;i<10;i++){ String uuid = UUID.randomUUID().toString().replaceAll("-", " ...

  5. [LintCode] Simplify Path 简化路径

    Given an absolute path for a file (Unix-style), simplify it. Have you met this question in a real in ...

  6. 【LeetCode】71. Simplify Path 解题报告(Python)

    [LeetCode]71. Simplify Path 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  7. 56. Edit Distance && Simplify Path

    Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...

  8. leetcode面试准备:Simplify Path

    leetcode面试准备:Simplify Path 1 题目 Given an absolute path for a file (Unix-style), simplify it. For exa ...

  9. 【LeetCode】71. Simplify Path

    Simplify Path Given an absolute path for a file (Unix-style), simplify it. For example,path = " ...

随机推荐

  1. Lua date转秒数

    之前写过一篇关于把秒转换成指定的日期格式 Lua date format   接到一个需求,需要从配置文件中读取活动显示时间段:startDate ~ endDate(格式为:yyyy-mm-dd H ...

  2. ios成长之每日一遍(day 6)

    toolBar上的View Switcher BIDAppDelegate.h #import <UIKit/UIKit.h> @class BIDSwitchViewController ...

  3. 怎样把vector和string数据传给旧的C API

     通常情况下.旧的C API使用数组合char*指针来进行数据交换而不是vector或string对象. 这种API还将存在非常长的一段时间,假设我们想有效地使用STL.我们就必须与它们和平共处. ...

  4. 《“胡”说IC——菜鸟工程师完美进阶》

    <“胡”说IC——菜鸟工程师完美进阶> 基本信息 作者: 胡运旺 出版社:电子工业出版社 ISBN:9787121229107 上架时间:2014-5-15 出版日期:2014 年5月 开 ...

  5. 3分钟搞定SpringBoot+Mybatis+druid多数据源和分布式事务

    文章来自: https://blog.csdn.net/qq_29242877/article/details/79033287 在一些复杂的应用开发中,一个应用可能会涉及到连接多个数据源,所谓多数据 ...

  6. Could not execute method of the activity Android

    导致此问题的原因有, 一:未注册 如果是 ActivityNotFoundException 的,那说明没在 Manifest.xml 的 application 标签下注册 activity. 二: ...

  7. HTML 5 <script> async 属性简单设置代码异步执行

    HTML5中 script标签支持脚本的异步执行async.脚本将会异步运行: <script type="text/javascript" src="demo_a ...

  8. PHP读写INI文件

    读INI文件 public function readini($name) { if (file_exists(SEM_PATH.'init/'.$name)){ $data = parse_ini_ ...

  9. 如何: 重命名在 IIS 6.0 中的虚拟目录

    警告如果错误地为编辑元数据库,您会导致严重的问题,甚至可能需要重新安装使用元数据库的任何产品. Microsoft 不能保证可以解决问题,如果您错误地编辑元数据库产生.编辑元数据库需要您自担风险. 注 ...

  10. 安装Lync 2013过程中遇到的第一个报错

    安装Lync 2013, 首先要去做的就是prepare AD Forest. 在使用向导的时候会遇到报错如下: Prepare Forest Active Directory setting exe ...