Leetcode Simplify Path
Given an absolute path for a file (Unix-style), simplify it.
For example,
path = "/home/"
, => "/home"
path = "/a/./b/../../c/"
, => "/c"
- 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"
.
题目不难,主要考虑一些特殊情况。
对于path = "/a/./b/../../c/"
, => "/c",模拟一下
先按照'/'对字符串进行分割,得到 [a, . , b, .. , .. , c]
首先进入目录a,注意 '.' 代表当前目录 ,".."代表上一个目录
然后到达'.',还是在当前目录,/a
然后到达'b',这为/a/b
然后到达'..',这是回到父目录,则变为/a
然后到达'..',继续回到父目录,则变为/
然后到达'c',则达到子目录,变为/c
- class Solution {
- public:
- vector<string> split(string& path, char ch){
- int index = ;
- vector<string> res;
- while(index < path.length()){
- while(index < path.length() && path[index] == '/') index++;
- if(index >= path.length()) break;
- int start=index, len = ;
- while(index < path.length() && path[index]!='/') {index++;len++;}
- res.push_back(path.substr(start,len));
- }
- return res;
- }
- string simplifyPath(string path) {
- vector<string> a = split(path,'/');
- vector<string> file;
- for(int i = ; i < a.size(); ++ i){
- if(a[i] == ".." ){
- if(!file.empty()) file.pop_back();
- }
- else if(a[i]!=".") file.push_back(a[i]);
- }
- string res="";
- if(file.empty()) res ="/";
- else{
- for(int i = ; i < file.size(); ++ i) res+="/"+file[i];
- }
- return res;
- }
- };
Leetcode Simplify Path的更多相关文章
- [LeetCode] Simplify Path 简化路径
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- [leetcode]Simplify Path @ Python
原题地址:https://oj.leetcode.com/problems/simplify-path/ 题意: Given an absolute path for a file (Unix-sty ...
- [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. For example,path = "/home/", ...
- 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 = " ...
- [LintCode] Simplify Path 简化路径
Given an absolute path for a file (Unix-style), simplify it. Have you met this question in a real in ...
- 56. Edit Distance && Simplify Path
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
随机推荐
- Maven总结
项目管理构建工具:maven ant gradle == 项目管理利器(Maven)——maven介绍及环境搭建maven可以帮助我们更有效地管理项目,它也是一套强大的自动化构建工具,覆盖了编译.测试 ...
- hadoop源码编译——2.5.0版本
强迫症必治: WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using b ...
- App- 借书趣
借书趣是一款方便用户在上海图书馆借书助手应用.通过扫描条码,导入豆瓣想读等手段可以方便管理想读想借的书目应用通过上图的接口和一些算法帮助用户生成借书单,规划用户可以去上图的哪个分馆可以借到最多想要阅读 ...
- 《UNIX网络编程(第3版)》unp.h等源码文件的编译安装
操作系统:Mac OS X 10.11.5 1.下载书中的源代码:点击下载 2.切换到解压后的目录 unpv13e,先查看下 README,依次执行: ./configure cd lib make ...
- class Solution(object): def fizzBuzz(self, n): a = [] i = 1 while(i <= n): if(i%15 == 0): a.append("FizzBuzz") elifleetcode day_01
412. Fizz Buzz Write a program that outputs the string representation of numbers from 1 to n. But fo ...
- 【Alpha版本】冲刺-Day5
队伍:606notconnected 会议时间:11月13日 会议总结 张斯巍(433) 今天安排:完成昨天没完成的,设置界面设计 完成度:85% 明天计划:学习UI设计 遇到的问题:无 感想:一定要 ...
- C++,当类名和对象名称相同时会发生什么?
今天突发奇想,如果类名和由这个类声明的对象标识符相同时会发生什么,然后就测试了一下.如下: #include <iostream> using namespace std; class a ...
- Android刷机教程
我的机器是Nexus 5 一. 安装驱动 如何进入fastboot模式 1. 拔掉数据线,将手机关机 2. 关机后同时按住[音量减小键]和[开关机键]即可进入Fastboot模式 开启usb调试 - ...
- 音频指纹(Philips)
参考<A Highly Robust Audio Fingerprinting System> Philips 音频指纹提取流程: 仿真效果: 第一个图为歌曲1的第一个指纹. 第二个图为歌 ...
- angular $http 与form表单的select
产品线 产品 版本 代码是联动关系 ng-model 绑定数据 设置默认值 ng-options 填充option ng-change 选项变化时的操作截图如下: html <!DOCTYPE ...