Leetcode71. Simplify Path简化路径
给定一个文档 (Unix-style) 的完全路径,请进行路径简化。
例如,
path = "/home/", => "/home"
path = "/a/./b/../../c/", => "/c"
边界情况:
- 你是否考虑了 路径 = "/../" 的情况?
在这种情况下,你需返回 "/" 。
- 此外,路径中也可能包含多个斜杠 '/' ,如 "/home//foo/" 。
在这种情况下,你可忽略多余的斜杠,返回 "/home/foo" 。
这道题的要求是简化一个Unix风格下的文件的绝对路径。
字符串处理,".."是返回上级目录(如果是根目录则不处理),重复连续出现的'/',只按1个处理, 如果路径名是".",则不处理;
class Solution {
public:
string simplifyPath(string path)
{
int len = path.size();
string str = "";
stack<string> q;
for(int i = 0; i < len; i++)
{
if(i == len - 1 && path[i] != '/')
str += path[i];
if(path[i] == '/' && str == "")
continue;
else if(path[i] == '/' || i == len - 1)
{
if(str == "..")
{
if(!q.empty())
q.pop();
}
else if(str == ".")
{
}
else
{
q.push(str);
}
str = "";
}
else
str += path[i];
}
string res = "";
while(!q.empty())
{
res = q.top() + res;
res = "/" + res;
q.pop();
}
if(res == "")
return "/";
return res;
}
};
Leetcode71. Simplify Path简化路径的更多相关文章
- lintcode 中等题:Simplify Path 简化路径
题目 简化路径 给定一个文档(Unix-style)的完全路径,请进行路径简化. 样例 "/home/", => "/home" "/a/./b ...
- [LintCode] Simplify Path 简化路径
Given an absolute path for a file (Unix-style), simplify it. Have you met this question in a real in ...
- [LeetCode] 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. Or in other words, convert it to the ca ...
- 071 Simplify Path 简化路径
给定一个文档 (Unix-style) 的完全路径,请进行路径简化.例如,path = "/home/", => "/home"path = " ...
- [LeetCode] 71. Simplify Path 简化路径
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- Simplify Path(路径简化)
问题: 来源:https://leetcode.com/problems/simplify-path Given an absolute path for a file (Unix-style), s ...
- LeetCode OJ:Simplify Path(简化路径)
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- 71. Simplify Path
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
随机推荐
- React弹窗组件
原文地址 小寒的博客 这里的弹窗泛指所有的弹出组件,这些组件不受页面其他UI布局影响,处于DOM结构的顶层,绝对定位在body元素下. 这个特殊性也给它的开发提出了特殊的要求. react新版本中的c ...
- JS的 try catch 前端使用场景(尽管不多还是会用到)
try{ //正常执行 }catch(e/*你感觉会出错的 错误类型*/){ // 可能出现的意外 eg:用户自己操作失误 或者 函数少条件 不影响下面的函数执行 // 有时也会用在 比如 focus ...
- 清空标签间的内容(innerHTML)和 value
jquery 方式: 清空标签的innerHTML: $("#divId").html(""); 清空标签的value: $("#divId" ...
- PHP面向对象魔术方法基本了解
简单介绍 (1) 魔术方法都是系统提供,程序员使用即可. (2) 所有的魔术方法,前面都是以 __ 开头的 _是两个下划线. (3) 我们在自定义函数时,就不要使用 __开头了. (4) 魔术方法是 ...
- PAT甲级——A1095 Cars on Campus
Zhejiang University has 8 campuses and a lot of gates. From each gate we can collect the in/out time ...
- Everything-启用http服务器(公网IP)会导致共享文件被搜索引擎搜索
1. 漏洞利用成功的前提 公网ip 启用http服务器 2.产生原因 启用http服务器功能点:让用户在本地或局域网上的其他电脑使用浏览器进行搜索,并支持文件下载.若拥有公网IP的用户启用此功能,就是 ...
- Chapter 3 树与二叉树
Chapter 3 树与二叉树 1- 二叉树 主要性质: 1 叶子结点数 = 度为2的结点数 + 1 2 二叉树第i层上最多有 (i≥1)个结点 3 深度为k的二叉树最多有 个结点 ...
- vue页面刷新数据丢失问题
参考: https://blog.csdn.net/aliven1/article/details/80743470 https://blog.csdn.net/liang37712 ...
- [转]WPF中的动画
WPF中的动画 周银辉 动画无疑是WP ...
- Django项目:CRM(客户关系管理系统)--56--47PerfectCRM实现CRM客户报名流程01
#urls.py """PerfectCRM URL Configuration The `urlpatterns` list routes URLs to views. ...