题目描述

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. Android Asynchronous Http Client-Android异步网络请求客户端接口

    1.简介 Android中网络请求一般使用Apache HTTP Client或者采用HttpURLConnect,但是直接使用这两个类库需要写大量的代码才能完成网络post和get请求,而使用and ...

  2. 开启Remote Desktop的PowerShell

    1) Enable Remote Desktop set-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Ser ...

  3. Minimum Path Sum leetcode java

    题目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right w ...

  4. java 使用AXIS调用远程的web service

    1.服务 2.代码 import javax.xml.namespace.QName; import org.apache.axis.client.Call; import org.apache.ax ...

  5. 【ElasticSearch】ElasticSearch-索引优化-自定义索引

    ElasticSearch-索引优化-自定义索引 es 指定 索引 字段_百度搜索 [es]创建索引和映射 - 匡子语 - 博客园 reindex,增加字段,并新增数据 - Elastic中文社区 e ...

  6. 如何导入另一个 Git库到现有的Git库并保留提交记录

    问题描述: 我在本地有两个Git库项目(D1=PC项目 包含通用项目,D2=移动项目 也包含通用项目这两个项目在同一目录下),因为这两个项目使用的通用项目是一样的如数据库访问等   只有显示层(vie ...

  7. MFC自定义控件如何向父窗口发送自定义消息

    自定义了一个控件 class CHtmlEditCtrlEx : public CHtmlEditCtrl   想在这个控件接收到Ctrl+V键盘消息的时候,向该控件所在的窗口发送一个自定义消息.具体 ...

  8. CHtmlEditCtrl (3): More HTML Editor Options

    In this version of our HTML Editor, we'll create a floating source view/edit window and we'll implem ...

  9. Express application generator的使用

    首先拷贝express官网的一篇文章: (http://expressjs.com/en/starter/generator.html ) Express application generator ...

  10. [Docker] Getting Started with Container Networks

    It is possible to group containers into a network and we can create multi networks so that container ...