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. Eclipse中Egit冲突解决

    Eclipse中Egit冲突解决 Git 作为进来最流行的分布式版本控制软件来说应用的十分广泛.EGit就是一款Eclipse上的Git插件.在使用Egit提交项目时,有时会产生冲突,需要对代码进行m ...

  2. String的常规使用集合

    今天先附上代码君: package com.jacob.javase; import java.io.UnsupportedEncodingException; /* *探讨String:  *  * ...

  3. ios多语言设置,操作

    多语言在应用程序中一般有两种做法:一.程序中提供给用户自己选择的机会: NSArray *languages = [NSLocale preferredLanguages]; NSString *cu ...

  4. 横向滚动条展示 css

    <div class="shuaixuan" style="overflow:hidden;">    <div style="ov ...

  5. jquery节点操作

    很久没有jquery写东西了,最近使用jquery的时候发现很多节点的操作都不太熟悉了,于是就进行了一个小小的总结. 创建节点:var dom=$('<div></div>') ...

  6. jQuery中的ajax服务端返回方式详细说明

    http://blog.sina.com.cn/s/blog_6f92e3a70100u3b6.html     上次总结了下ajax的所有参数项,其中有一项dataType是设置具体的服务器返回方式 ...

  7. Ansible安装配置及使用

    一.Ansible特点 1.不需要安装客户端,通过sshd通信 2.基于模块工作,模块可以由任何序言开发 3.不仅支持命令行使用模块,也支持编写yaml格式的playbook 4.支持sudo 5.有 ...

  8. Java——日期格式

     /* * 日期对象和毫秒值之间的转换. * * 毫秒值--->日期对象: *  1.通过Date对象的构造方法new Date(timeMillis) *  2.还可以通过setTime设 ...

  9. VC调用系统的调色板

    调用系统的调色板可以用到ChooseColor这个函数,这个函数传入一个参数为CHOOSECOLOR结构体的指针,其函数原型为 BOOL ChooseColor(LPCHOOSECOLOR lpCC) ...

  10. Android Studio + gradle多渠道打包

    通过工具栏的Build->Build Apk 好像只能打包第一个Module(eclipse里面是Project的概念),怎么多渠道打包呢?目前好像只能一个一个的打 首先在清单文件里设置个变量: ...