leetcode Simplify Path

leetcode Simplify Path

Given an absolute path for a file (Unix-style), simplify it.

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

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

题目地址:leetcode Simplify Path

题意:给定unix路径,要求进行简化。

思路:unix路径主要是用’/’进行区分 一个点表示当前目录,两个点表示上级目录。

可以按照 ‘/’进行划分,如果遇到’..’就数组中移出最后一个(就是个栈嘛),遇到‘.’就无视吧~

C++没有split函数,于是就来硬的。。(也可以用getline(ss, cur, ‘/’)) 来划分

和Python相比。。py好简洁。

 

C++ getline version

 

C++

 

Python

 

本博客若无特殊说明则由 hrwhisper 原创发布
转载请点名出处:细语呢喃 > leetcode Simplify Path
本文地址:https://www.hrwhisper.me/leetcode-simplify-path/

打赏一杯咖啡钱呗

codes, Leetcode , , , . permalink.

2 thoughts on “leetcode Simplify Path

  1. stringstream ss(path);
    while (getline(ss, tmp, ‘/’))

    c++应该可以这样用分割函数?

    楼主很强啊,希望结识楼主。方便的话邮件交流下联系方式^_^

Leave a Reply

Your email address will not be published. Required fields are marked *