leetcode[70] Simplify Path
题目的意思是简化一个unix系统的路径。例如:
path = "/home/", => "/home"
path = "/a/./b/../../c/", => "/c"
我尝试用逐个字符判断的方法,一直提交测试,发现要修改甚多的边界。于是就参考了这位大神
思路其实不会那么复杂,C#里面的话直接可以用split就可以分割string,c++中好像要委婉实现,例如 getline(ss,now,'/')
在c++中getline(istream& is, string& str, char delim)
Extracts characters from is and stores them into str until the delimitation character delim is found
是指将is中的直到下一个delim字符间的数据给str,delim不会在str中,如果delim缺省,那么默认‘\n'
因为文件流是往后读,读到文件末尾为止,所以用while来处理,详见代码。
class Solution {
public:
string simplifyPath(string path) {
string ans,now;
vector<string> list;
stringstream ss(path);
while(getline(ss,now,'/'))
{
if(now.length()== || now==".")
continue;
if(now=="..")
{
if(!list.empty())
list.pop_back();
}
else
{
list.push_back(now);
}
}
for(int i=; i<list.size(); i++)
{
ans += "/";
ans += list[i];
}
if(ans.length()==) ans = "/";
return ans;
}
};
leetcode[70] Simplify Path的更多相关文章
- [LeetCode] 71. Simplify Path 简化路径
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- 【leetcode】Simplify Path
题目简述: Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/& ...
- Java for LeetCode 071 Simplify Path
Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", ...
- Leetcode 之Simplify Path @ python
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- Leetcode#71 Simplify Path
原题地址 用栈保存化简后的路径.把原始路径根据"/"切分成若干小段,然后依次遍历 若当前小段是"..",弹栈 若当前小段是".",什么也不做 ...
- Leetcode 之Simplify Path(36)
主要看//之间的内容:如果是仍是/,或者是.,则忽略:如果是..,则弹出:否则压入堆栈.最后根据堆栈的内容进行输出. string simplifyPath(string const& pat ...
- leetcode面试准备:Simplify Path
leetcode面试准备:Simplify Path 1 题目 Given an absolute path for a file (Unix-style), simplify it. For exa ...
- 【LeetCode】71. Simplify Path 解题报告(Python)
[LeetCode]71. Simplify Path 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】71. Simplify Path
Simplify Path Given an absolute path for a file (Unix-style), simplify it. For example,path = " ...
随机推荐
- Android中部署自己的su
本人博客原文 首先把你的自己的su的放到Android应用程序project的assets文件夹,为了和系统的su区分,我自己的su文件叫做sur. 另外我这里没有考虑x86架构的cpu的手机. 废话 ...
- JavaScript之二:this
在JavaScript中,this的指代对象是什么?最精辟的解释却只有一句话: when a function of an object was called, the object will be ...
- github中origin和upstream的区别(转)
Fork,本身并不是git工具中的一个命令,也不是对git的扩展,它是在GitHub上的概念,是另一种clone方式——在服务器端的clone.而我们通常意义上的clone,是将远程repo 复制一份 ...
- SQLServer 存储过程嵌套事务处理
原文:SQLServer 存储过程嵌套事务处理 某个存储过程可能被单独调用,也可能由其他存储过程嵌套调用,则可能会发生嵌套事务的情形. 下面是一种解决存储过程嵌套调用的通用代码,在不能确定存储过程是否 ...
- jQuery地图热点效应-后在弹出的提示鼠标层信息
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- 敏感字符串加密处理(PHP实现)
/** * 敏感字符串加密处理 * @param $raw_str 原始字符串 * @param $before 前面保留的显示位数 * @param $after 后面保留的显示位数 * @para ...
- JavaEE(8) - 本地和远程调用的有状态以及无状态Session EJB
1. 使用NetBeans开发Session Bean #1. 创建项目:File-->New Project-->Java EE-->EJB Module #2. 在项目中创建Se ...
- 考试easy该,学习如何做?
我的两个学生(场和任)都讲了他们周末參加的一个认证考试不考大题考小题的事情.由感而发: 话说不用大题考,大概是不敢用大题考. 老师的教.和学生的学中.存在的一些问题得不到解决,整体讲,学生的学习效果没 ...
- 集成 Entity Framework
ABP 基础设施层——集成 Entity Framework 本文翻译自ABP的官方教程<EntityFramework Integration>,地址为:http://aspnetboi ...
- js面向对象的学习笔记九(BOM 与 DOM 经常使用的属性分析)
一 BOM物 window 的 相关属性 1. 用户配置的机器配置对象 navigator navigator.userAgent //该属性能够查看用户机器浏览器的配置 "Mozilla ...