LeetCode Simplify Path

2014-11-24 07:42:53 · 作者: · 浏览: 0

Simplify Path

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". 这也属于难题吧,不过是一种特别的难题,或者算是很繁的难题。

    但是对于我来说就是,需要搞清楚他到底有多少特殊规定。

    比如题目不明显的规矩:

    1 如何处理/./--直接忽略

    2 如何处理/../--回退一个单词(目录)

    3 第一个字符必须是'/'

    还有明显的规矩:

    4 双//处理

    5 结尾单词后面不能接'/'

    搞清楚之后就好写程序了,就是把一个一个规矩实现,写入新的字符串中。

    这些问题都很细,需要耐心处理,而且每个人的处理方法好像都可以有点不一样,不过最主要的就是思路要清晰,不能乱。

    下面是注释详细的程序,不带注释就是一团麻了,注释好,思路还算清晰的。

    class Solution {
    public:
    	string simplifyPath(string path) 
    	{
    		string res;
    		//规矩1: 第一个必定是'/'
    		res.push_back('/');
    		for (int i = 1; i < path.length(); i++)
    		{
    			string ts = "";
    			//查找/   /之间的单词,保存在ts中
    			while (i