题目的意思是简化一个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. [Network]Transport Layer

    1 Principles behind Transport Layer Services 1.1 Multiplexing/Demultiplexing Multiplexing at sender ...

  2. Linux 字符驱动程序(一)

    Linux 字符驱动程序(一) 于linux有三个主要的内核设备: 1 字符设备:         •字符设备的读写以字节为单位,存取时没有缓存.      •对字符设备发出读写请求时.实际的硬件I/ ...

  3. 于PsIsSystemThread无论是在线程系统线程标识获得

    我一直好奇一个进程的所有线程改变线程标志Terminated mov edi, edi ; IoIsSystemThread push ebp mov ebp, esp mov eax, [ebp+a ...

  4. UVA 1364 - Knights of the Round Table (获得双连接组件 + 二部图推理染色)

    尤其是不要谈了些什么,我想A这个问题! FML啊.....! 题意来自 kuangbin: 亚瑟王要在圆桌上召开骑士会议.为了不引发骑士之间的冲突. 而且可以让会议的议题有令人惬意的结果,每次开会前都 ...

  5. C#使用ServiceController控制windows服务

    C#在,使用ServiceController控制类windows服务,添加首次使用前引文:System.ServiceProcess,空间中引用:using System.ServiceProces ...

  6. Javascript标准类型的方法集

    1 array.concat(item...) concat方法会产生一个新数组,将一个或多个item附加在数组之后 var a = ['a', 'b', 'c'] var b = ['x', 'y' ...

  7. 怎样改动、扩展并重写Magento代码

    作为一个开发人员的你,肯定要改动Magento代码去适应你的业务需求,可是在非常多时候我们不希望改动Magento的核心代码,这里有非常多原因, 比如将来还希望升级Magento.还想使用很多其它的M ...

  8. listener.ora中PLSExtPro 和ExtProc的作用(转)

    默认安装时,会安装一个PL/SQL外部程序(ExtProc)条目在listener.ora中,是oracle为调用外部程序默认配置的监听,它的名字通常是ExtProc或PLSExtProc,但一般不会 ...

  9. nyoj 破门锁(水题)

    Time Limit: 1000ms Memory Limit: 128000KB 64-bit integer IO format:      Java class name: Submit Sta ...

  10. LwIP学习笔记——STM32 ENC28J60移植与入门

    0.前言     去年(2013年)的整理了LwIP相关代码,并在STM32上"裸奔"成功.一直没有时间深入整理,在这里借博文整理总结.LwIP的移植过程细节很多,博文也不可能一一 ...