博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Simplify Path
阅读量:4686 次
发布时间:2019-06-09

本文共 1114 字,大约阅读时间需要 3 分钟。

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".
 
Runtime: 6ms
1 class Solution { 2 public: 3     string simplifyPath(string path) { 4         string newPath; 5         vector
directory; 6 stringstream newString(path); 7 string temp; 8 while (getline(newString, temp, '/')) { 9 if (temp == "." || temp == "") continue;10 else if (temp == ".." && !directory.empty()) directory.pop_back();11 else if (temp != "..") directory.push_back(temp);12 }13 14 for (string current : directory) {15 newPath += "/" + current;16 }17 return newPath.empty() ? "/" : newPath;18 }19 };

 

转载于:https://www.cnblogs.com/amazingzoe/p/5919169.html

你可能感兴趣的文章
python中读取配置文件的方式
查看>>
spoj11814
查看>>
IntelliJ IDEA - 热部署插件JRebel ,对静态资源文件进行热部署?javascript、css、vm文件...
查看>>
Oracle优化器和优化模式
查看>>
C# Linq 笛卡尔积
查看>>
java六个必须理解的问题+java学习方法
查看>>
2017南宁现场赛E 存档
查看>>
关于朋友之间借钱
查看>>
wpf企业应用之主从结构列表
查看>>
AngularJS 承诺 Promise
查看>>
CDN全局流量调度算法介绍
查看>>
Word 2007 目录生成技巧
查看>>
linux网络协议栈--路由流程分析
查看>>
weblogic.jms.common.MessageFormatException: JMSClientExceptions: Invalid property name
查看>>
python3使用urlllib爬虫1
查看>>
值得收藏的十二条Jquery随身笔记
查看>>
当DiscuzNT遇上了Loadrunner(下)(转)
查看>>
mysql学习之join用法
查看>>
UIScrollView+UIPageControl 图片切换加分页标示
查看>>
【未有之有】洛依文明相关
查看>>