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/", ...
随机推荐
- PAT甲级——A1099 Build A Binary Search Tree
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- js时间操作getTime(),ios移动端真机上返回显示NAN
ios移动端,js时间操作getTime(),getFullYear()等返回显示NaN的解决办法及原因 在做移动端时间转化为时间戳时,遇到了一个问题,安卓手机上访问时,能拿到时间戳,从而正确转换时间 ...
- C语言开发系列-二进制
n位二进制的取值范围 -2的n-1次方 ~ 2的n-1次方-1 输出一个整数的二进制的存储形式 #include <stdio.h> // 输出一个整数的二进制的存储形式 void put ...
- Top- Linux必学的60个命令
1.作用 top命令用来显示执行中的程序进程,使用权限是所有用户. 2.格式 top [-] [d delay] [q] [c] [S] [s] [i] [n] 3.主要参数 d:指定更新的间隔,以秒 ...
- parameter– tRPRE and tRPST
DDR读数据有效之前,有一段时间DQS(DQS#)需为低(高),此段时间即为read preamble,tRPRE. 同理,读数据结束之前,某段时间为read postamble,tRPST.
- js笔试-接收get请求参数
请编写一个JavaScript函数,它的用途是接收url中get请求的参数,并返回为对象, 如: var url = “https://i.cnblogs.com/EditPosts.aspx?opt ...
- java锁_IO_NIO_AIO_BIO_GC_Jvm
如何保证线程安全,线程锁有哪些? 同步方法和同步代码块常见的锁:ReentrantLock与synchronized二者区别: (1) 线程A和B都要获取对象O的锁定,假设A获 ...
- KOA 学习(八) koa-bodyparser
此控件支持Josn,form,text类型 用法 var Koa = require('koa'); var bodyParser = require('koa-bodyparser'); var a ...
- js判断两个对象是否相等
function isObjectValueEqual(a, b) { if(typeof a == 'number' && typeof b == 'number'){ return ...
- 使用 windows 批处理指令(BAT文件)进行文件删除、复制操作
以下是做文件删除和复制的批处理指令 ::替换文件需要添加 /y 参数才能直接替换.不然会出现提示是否替换. ::复制Axis2Implementation和WebServices编译后的文件到tomc ...