题目的意思是简化一个unix系统的路径。例如:

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

我尝试用逐个字符判断的方法,一直提交测试,发现要修改甚多的边界。于是就参考了这位大神

思路其实不会那么复杂,C#里面的话直接可以用split就可以分割string,c++中好像要委婉实现,例如 getline(ss,now,'/')

在c++中getline(istream& is, string& str, char delim)

Extracts characters from is and stores them into str until the delimitation character delim is found

是指将is中的直到下一个delim字符间的数据给str,delim不会在str中,如果delim缺省,那么默认‘\n'

因为文件流是往后读,读到文件末尾为止,所以用while来处理,详见代码。

(1)用“/”分割字符串,遍历每个分割部分,存入一个vector<string>中
(2)若当前分割部分为空,证明有连续的"/"或是最后一个“/”,忽略
(3)若当前部分为“.”,忽略
(4)若当前部分为“..”,若vector不为空,去除vector最后一个元素
(5)再将vector中的string用“/”连起来,得到结果
class Solution {
public:
string simplifyPath(string path) {
string ans,now;
vector<string> list;
stringstream ss(path);
while(getline(ss,now,'/'))
{
if(now.length()== || now==".")
continue;
if(now=="..")
{
if(!list.empty())
list.pop_back();
}
else
{
list.push_back(now);
}
}
for(int i=; i<list.size(); i++)
{
ans += "/";
ans += list[i];
}
if(ans.length()==) ans = "/";
return ans;
}
};

leetcode[70] Simplify Path的更多相关文章

  1. [LeetCode] 71. Simplify Path 简化路径

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

  2. 【leetcode】Simplify Path

    题目简述: Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/& ...

  3. Java for LeetCode 071 Simplify Path

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

  4. Leetcode 之Simplify Path @ python

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

  5. Leetcode#71 Simplify Path

    原题地址 用栈保存化简后的路径.把原始路径根据"/"切分成若干小段,然后依次遍历 若当前小段是"..",弹栈 若当前小段是".",什么也不做 ...

  6. Leetcode 之Simplify Path(36)

    主要看//之间的内容:如果是仍是/,或者是.,则忽略:如果是..,则弹出:否则压入堆栈.最后根据堆栈的内容进行输出. string simplifyPath(string const& pat ...

  7. leetcode面试准备:Simplify Path

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

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

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

  9. 【LeetCode】71. Simplify Path

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

随机推荐

  1. Java Web项目结构

    Java Web项目结构(一般) 1.Java src 2.JRE System Library 3.Java EE 6 Libraries 4.Web App Libraries 5.WebRoot ...

  2. 实现Asp.net Mvc分布式Session Redis群集

    Redis群集实现Asp.net Mvc分布式Session Session的缺点 众所周知Asp.net Session默认存储在IIS中,IIS的重启会导致Session丢失. 如果你的网站使用了 ...

  3. POJ 3982 序列 塔尔苏斯问题解决

    而且还加入了大量的主题,直接或模板Java我们能够在水. 除了循环33它的时间,计算A99它是第几,输出准确回答. #include <stdio.h> #include <stri ...

  4. Model绑定

    Model绑定 在前面的几篇文章中我们都是采用在URI中元数据类型进行传参,实际上ASP.NET Web API也提供了对URI进行复杂参数的绑定方式--Model绑定.这里的Model可以简单的理解 ...

  5. [Unity3D]Unity3D圣骑士当游戏开发商遭遇Mecanim动画系统

            大家好.我是秦培.欢迎关注我的博客.我的博客地址blog.csdn.net/qinyuanpei. 博主总算赶在这个月底写出了这篇文章.这个月由于期末考试一直没时间研究太多关于技术方面 ...

  6. 脚本+批处理打造IIS监控器

    原文 脚本+批处理打造IIS监控器 首先说下我什么要写它,第一.它可以帮你做一件事,那就是随时给你监控你公司的网站服务器的状态,一旦你的网站出现问题不能访问了,它就会自动帮你重启IIS然后让死掉的网站 ...

  7. 半平面交总结and模板

    博客原文地址:http://blog.csdn.net/xuechelingxiao/article/details/40859973 这两天刷了POJ上几道半平面交,对半平面交有了初步的体会,感觉半 ...

  8. POI使用汇总

    POI它是apache下一步行为office有关格源工具.我们近期在使用这个框架实现Excel格式数据的导入和导出功能.这里进行一个使用总结.POI提供两种读写API模型:事件模型(eventmode ...

  9. Android FM学习中的模块 FM启动过程

    最近的研究FM模,FM是一家值我正在学习模块.什么可以从上层中可以看出. 上层是FM按钮的操作和界面显示,因此调用到FM来实现广播收听的功能. 看看Fm启动流程:例如以下图: 先进入FMRadio.j ...

  10. 【Socket规划】套接字Windows台C语言

    [编译环境]:Visual Studio 2013 这是服务端实现流程. #include<stdio.h> #include<stdlib.h> #include<wi ...