【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 ...
随机推荐
- 安装win7 64位和win10 64位双系统小结
1.gpt比mbr更先进.与主启动记录 (MBR) 分区方法相比,GPT 具有更多的优点,因为它允许每个磁盘有多达 128 个分区(mbr只支持4个分区),支持高达 18 千兆兆字节的卷大小,允许将主 ...
- canvas猜数游戏
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- unity代码创建草和模拟风的效果
void Start() { Test4(); } //草 private Vector3[] grassArray = new Vector3[7]; private GameObject gras ...
- [作业] Python入门基础--用户登陆
让用户输入用户名密码 认证成功后显示欢迎信息 输错三次后锁定登陆 #__author:Mifen #date: 2018/11/28 import time #自定义本地用户名和密码 user_nam ...
- A GDB Tutorial with Examples--转
http://www.cprogramming.com/gdb.html A GDB Tutorial with Examples By Manasij Mukherjee A good debugg ...
- 基于开源Red5搭建的视频直播平台
开始之前,为了便于大家了解Red5,此处引用网络文字,非原创 引言 流媒体文件是目前非常流行的网络媒体格式之一,这种文件允许用户一边下载一边播放,从而大大减少了用户等待播放的时间.另外通过网络播放流媒 ...
- 《Centos服务器版安装教程》
安装前准备: (1) 首先大家需要在电脑上安装一个VMware (2) Centos7系列的一个服务器版镜像 有了这两样东西,下面我们就开始安装了 一. 打开VMware,新建一个虚拟机 ...
- [转] 常用的sql server规范
常见的字段类型选择 1.字符类型建议采用varchar/nvarchar数据类型 2.金额货币建议采用money数据类型 3.科学计数建议采用numeric数据类型 4.自增长标识建议采用bigi ...
- [javaSE] 数据结构(二叉查找树-插入节点)
二叉查找树(Binary Search Tree),又被称为二叉搜索树,它是特殊的二叉树,左子树的节点值小于右子树的节点值. 定义二叉查找树 定义二叉树BSTree,它保护了二叉树的根节点BSTNod ...
- 一:SpringCloud
一:前提知识+相关说明 前提知识:springmvc+spring/springboot+mybatis+maven+git...... cloud技术的五大神兽: 面试题: 什么是微服务? 微服务之 ...