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,能够将  多了斜杠///   简化为一个/

=================

思路:

这里需要一个字符串划分函数,c++中是没有提供字符串划分函数的.

可以参考[ http://www.cnblogs.com/li-daphne/p/5524752.html ]分析过程.

主要是利用了string类中的find,substr函数

----

对输入字符串做slipt之后,所有的路径名字包括[.]和[..]都会存入一个vector中,

此后,我们再利用栈来剔除[.]和[..]路径,

最后一次对栈中的数据进行处理就好了.

============

代码如下:

void myslipt(string &s,vector<string> &re,string &c){
std::string::size_type pos1,pos2;
pos2 = s.find(c);///find
pos1 = ;
while(std::string::npos != pos2){
string t = s.substr(pos1,pos2-pos1);///[p1,p2)
if(!t.empty()){
re.push_back(t);
}
pos1 = pos2+c.size();
pos2 = s.find(c,pos1);
}
if(pos1!=s.length()){
re.push_back(s.substr(pos1));
}
} string simplifyPath(string path) {
vector<string> re;
string c = "/";
myslipt(path,re,c);
stack<string> st;
for(size_t i = ;i<re.size();i++){
if(re[i]==".") continue;
else if(re[i]==".."){
if(st.empty()){
continue;
}else{
st.pop();
}///if-else
}else{
st.push(re[i]);
}
}
string result;
while(!st.empty()){
result.insert(,st.top());
result.insert(,"/");
st.pop();
}
return result;
}

71. Simplify Path的更多相关文章

  1. 71. Simplify Path(M)

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

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

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

  3. 【LeetCode】71. Simplify Path

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

  4. 【一天一道LeetCode】#71. Simplify Path

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  5. 71. Simplify Path (Stack)

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

  6. 71. Simplify Path压缩文件的绝对路径

    [抄题]: Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/&q ...

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

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

  8. [LC] 71. Simplify Path

    Given an absolute path for a file (Unix-style), simplify it. Or in other words, convert it to the ca ...

  9. Leetcode#71 Simplify Path

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

随机推荐

  1. 147. Insertion Sort List

    Sort a linked list using insertion sort. 代码如下: /** * Definition for singly-linked list. * public cla ...

  2. 推荐一些android开发学习的资料

    网址: 1:http://v.youku.com/v_show/id_XMTgwMTQ1MTgw.html 2:http://mars.apkbus.com/ 3:http://wenku.baidu ...

  3. react 不能往组件中传入属性的值为 undefined

    在使用 andt design 的时候遇到个需求,需要清除 Select 组件选中后的值,让它变成什么都没选中,显示 placeholder 刚开始以为设置为 null 即可,结果发现设置为 null ...

  4. Linux系统编程@进程通信(一)

    进程间通信概述 需要进程通信的原因: 数据传输 资源共享 通知事件 进程控制 Linux进程间通信(IPC)发展由来 Unix进程间通信 基于System V进程间通信(System V:UNIX系统 ...

  5. 黑马程序员——JAVA基础之System,Runtime,Date,Calendar,Math

    ------- android培训.java培训.期待与您交流! ---------- System:  类中的方法和属性都是静态的. out:  标准输出,默认是控制台. in:标准输入,默认是键盘 ...

  6. [原创]cocos2d-x研习录—前言

    我认为很多开发者面对层出不穷的新技术.新思想和新理念,最为之苦恼的是找不到行之有效的学习方法,对于知识的本质缺乏认识,虽阅读了大量教材,却无法将其融入自己的知识体系,并搭建自己的知识树.不可否认,教材 ...

  7. 【转】iOS开发拓展篇—静态库

    原文网址:http://www.cnblogs.com/wendingding/p/3893095.html iOS开发拓展篇-静态库 一.简单介绍 1.什么是库? 库是程序代码的集合,是共享程序代码 ...

  8. postgresql downgrade issue

    Q: Dear Support Team, If we use ubuntu server to install postgresql9.4, how can we keep original dat ...

  9. SQL Server 索引分类

    什么是索引 拿汉语字典的目录页(索引)打比方:正如汉语字典中的汉字按页存放一样,SQL Server中的数据记录也是按页存放的,每页容量一般为4K .为了加快查找的速度,汉语字(词)典一般都有按拼音. ...

  10. iproute-2.6.32

    iproute之tc命令翻译地址,man tc的翻译 http://blog.csdn.net/ysdaniel/article/details/7905879