题目描述

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加密解密

    随笔分类 - Android加密解密 Android数据加密之异或加密算法 摘要: 前言: 这几天被公司临时拉到去做Android IM即时通信协议实现,大致看了下他们定的协议,由于之前没有参与,据说 ...

  2. Java反编译工具CFR,Procyon简介

    Java反编译工具有很多,个人觉得使用最方便的是jd-gui,当然jad也不错,jd-gui主要提供了图形界面,操作起来很方便,但是jd-gui很久没有更新了,java 7出来很久了,jd-gui在反 ...

  3. [转]让Nginx支持ThinkPHP的URL重写和PATHINFO

    From : http://www.jzxue.com/wangzhankaifa/php/201108/08-8396.html   ThinkPHP支持通过PATHINFO和URL rewrite ...

  4. jcseg-1.8.7版本发布 - 多配置适应+完整开发帮助文档

    jcseg是使用java开发的一款开源中文分词器, 并且提供了最新版本的lucene和solr分词接口. jcseg 1.8.7版本发布了: 1. 更改了内部设计, 加入JcsegTaskConfig ...

  5. 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 ...

  6. 人脸识别中的Procruster analysis应用

    本文中,我们通过Procrustes analysis来处理特征点,Procrustes analysis算法可以参考:http://en.wikipedia.org/wiki/Procrustes_ ...

  7. Log4cplus使用指南

    1.  Log4cplus简介 log4cplus是C++编写的开源的日志系统,前身是java编写的log4j系统,受Apache Software License保护,作者是Tad E. Smith ...

  8. 一次问题追查----短字符串签名算法引发的bug

    近期开发代码, 出现了一些诡异现象.追查原因是公司使用的签名函数出现的问题. 问题: 代码使用的签名库函数, 对于<=4字节的字符串, 签名就是本身. #include<stdio.h&g ...

  9. C# DevExpress TabPane【转】

    DevExpress TabPane能够快速简单地实现了Tab窗口的功能,相对XtraTabControl来说功能较为简单,也能够更快实现功能. 1.添加TabPane控件 将它拖到form即可. 2 ...

  10. Combination Sum II leetcode java

    题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...