【Leetcode】【Medium】Simplify Path
Given an absolute path for a file (Unix-style), simplify it.
For example,
path = "/home/", => "/home"
path = "/a/./b/../../c/", => "/c"
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".
解题:
题意为简化unix路径,即省略多余字符“/”和运算“.”、"..",返回unix的真实路径;
需要考虑的特殊情况是:
1、根目录下操作"..",还是根目录
2、连续两个“/”,可看做无任何作用
3、路径末尾的"/",要消除
思路:
每次取出两个“/”之间的字符,如果是"."或者空字符表示还在本层路径,如果是".."表示返回上一层路径,如果是其他字符,则表示下一层路径名;
用栈来保存每一层路径非常合适,但是后来变成过程中,使用vector来模拟栈操作,在最后整合最终路径时反而更方便。
代码:
class Solution {
public:
string simplifyPath(string path) {
string res, substr;
vector<string> stk;
stringstream ss(path);
while(getline(ss, substr, '/')) {
if (substr == ".." and !stk.empty())
stk.pop_back();
else if (substr != ".." && substr != "." && substr != "")
stk.push_back('/' + substr);
}
for (int i = ; i < stk.size(); ++i)
res += stk[i];
return res.empty() ? "/" : res;
}
};
【Leetcode】【Medium】Simplify Path的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- 【LeetCode每天一题】Simplify Path(简化路径)
Given an absolute path for a file (Unix-style), simplify it. Or in other words, convert it to the ca ...
- 【leetcode刷题笔记】Simplify Path
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- 【LeetCode每天一题】Minimum Path Sum(最短路径和)
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- 【leetcode刷题笔记】Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists
[Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...
- 【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman
[Q10] Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...
随机推荐
- 对 BOIS 的一些介绍
BIOS里不可调节CPU将在64/32位系统下工作 知识点延伸1,一般CPU要想在64位状态下运行必须具备两个条件: 1)安装64位操作系统 2)CPU本身就支持64位指令集 知识点延伸2有必要了解B ...
- R语言统计字符串的字符数ncahr函数
函数计算字符数量,包括在一个字符串的空格的个数. 语法 nchar()函数的基本语法是: nchar(x) 以下是所使用的参数的说明: x - 向量输入. 示例 result <- nchar( ...
- 费了个劲的git
终于学会了git,表示不容易,个人表示总结一下... 1.设置个人PC上的git master路径,ok没问题,用时1min 2.从码云代码仓库克隆已写代码,也很简单,用时2min 3.将本机器内文 ...
- IoC容器之Unity
关于IoC.Unity见博友文章点击这里. 话不多说,上程序HelloUnity,程序采用VS2010,Unity2.1. 1.程序框架如下 2.类库HelloUnity.Objects,主要为实体类 ...
- lodop判断是否打印成功
需要引用js <script src="js/jquery-3.3.1.js"></script> <script src="js/Lodo ...
- js实现点击图片,然后图片放大
HTML <td width="350"> <img height="100" width="100" class=&qu ...
- 浏览器获取正确的scrollTop值
window.pageYOffset 被所有浏览器支持除了 IE 6, IE 7, IE 8, 不关doctype的事, 注IE9 开始支持此属性. window.scrollY 被Firefox, ...
- JS实现最小生成树之克鲁斯卡尔(Kruskal)算法
克鲁斯卡尔算法打印最小生成树: 构造出所有边的集合 edges,从小到大,依次选出筛选边打印,遇到闭环(形成回路)时跳过. JS代码: //定义邻接矩阵 let Arr2 = [ [0, 10, 65 ...
- ssh 连接慢问题
连接先看报错: There were 11 failed login attempts since the last successful login. 先前有上百上千失败login,被攻击了,把短时 ...
- 16、IO (Properties、序列化流、打印流、CommonsIO)
Properties集合的特点 * A: Properties集合的特点 * a: Properties类介绍 * Properties 类表示了一个持久的属性集.Properties 可保存在流中或 ...