[leetcode] 【字符串】71. Simplify Path
2016-06-15 16:50
357 查看
Given an absolute path for a file (Unix-style), simplify it.
For example,
path =
path =
Did you consider the case where path =
In this case, you should return
Another corner case is the path might contain multiple slashes
such as
In this case, you should ignore redundant slashes and return
二是..表示进入父目录
用一个栈结构存储目录路径的每个文件夹,遇到..则出栈
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风格的绝对路径题解
有几种情况需要处理 一是多个/只当1个/处理二是..表示进入父目录
用一个栈结构存储目录路径的每个文件夹,遇到..则出栈
class Solution { public: string simplifyPath(string path) { stack<string> file; for(int i=0;i<path.size();) { while(path[i]=='/'&&i<path.size()) i++; string temp; while(path[i]!='/'&&i<path.size()) temp+=path[i++]; if(temp==".."&&!file.empty()) file.pop(); if(temp!="."&&temp!=".."&&temp!="") file.push(temp); } string res; if(file.empty()) res="/"; while(!file.empty()) { res=file.top()+res; res='/'+res; file.pop(); } return res; } };
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- C++中const用法总结
- CPP 虚函数、虚函数表及虚拟继承(转)
- leetcode----Longest Substring Without Repeating Characters
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- Deploying Control Plane Policing
- 021-Merge Two Sorted Lists(合并两个排好序的单链表);leetcode
- LeetCode[Day 1] Two Sum 题解