## 一.題目描述
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風格下的文件路徑。例如輸入代表路徑的字符串:”/home/”,簡化后為:”/home”;或輸入”/a/./b/../../c/”,簡化為:”/c”
字符串中,應該了解到”..”是返回到路徑的上級目錄(如果當前根目錄則不處理),這里使用棧來記錄路徑名。在處理字符串的過程中,有以下條件:
~~~
1\. 重復連續出現的'/',只按1個處理,即跳過重復連續出現的'/';
2\. 如果路徑名不為"."和"..",將記錄的字符串入棧;
3\. 如果路徑名是".."且棧不為空,則需要出棧,否則無需處理;
~~~
在遍歷字符串后,逐個取出棧中元素(即已保存的路徑名),用’/’分隔并拼接起來,注意取出的元素是從后往前連接的。
## 三.實例代碼
~~~
#include <iostream>
#include <stack>
#include <string>
using namespace std;
class Solution
{
public:
string simplifyPath(string path)
{
stack<string> str;
for (size_t i = 0; i < path.size(); ++i)
{
string name = "";
while (i < path.size() && path[i] == '/')
++i; // 該操作跳過斜線'/'
while (i < path.size() && path[i] != '/')
name += path[i++]; // 開始記錄路徑名,包括".."
if (name != "." && name != "..") // 對對不同的文件名進行不同操作
str.push(name); // 非".."文件名,壓棧
else if (!str.empty() && name == "..")
str.pop(); // 當前文件名為"..",表示退到上層目錄,需彈出棧
}
if (str.empty())
return "/";
string result = "";
while (!str.empty())
{
result = "/" + str.top() + result; // 最后組合path
str.pop();
}
return result;
}
};
~~~

## 四.小結
這道題使用了string類的一些操作,該類的一些功能確實非常強大。以下是關于string類的一些用法總結:?
[http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html](http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html)
- 前言
- 2Sum
- 3Sum
- 4Sum
- 3Sum Closest
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array II
- Search in Rotated Sorted Array
- Remove Element
- Merge Sorted Array
- Add Binary
- Valid Palindrome
- Permutation Sequence
- Single Number
- Single Number II
- Gray Code(2016騰訊軟件開發筆試題)
- Valid Sudoku
- Rotate Image
- Power of two
- Plus One
- Gas Station
- Set Matrix Zeroes
- Count and Say
- Climbing Stairs(斐波那契數列問題)
- Remove Nth Node From End of List
- Linked List Cycle
- Linked List Cycle 2
- Integer to Roman
- Roman to Integer
- Valid Parentheses
- Reorder List
- Path Sum
- Simplify Path
- Trapping Rain Water
- Path Sum II
- Factorial Trailing Zeroes
- Sudoku Solver
- Isomorphic Strings
- String to Integer (atoi)
- Largest Rectangle in Histogram
- Binary Tree Preorder Traversal
- Evaluate Reverse Polish Notation(逆波蘭式的計算)
- Maximum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Longest Common Prefix
- Recover Binary Search Tree
- Binary Tree Level Order Traversal
- Binary Tree Level Order Traversal II
- Binary Tree Zigzag Level Order Traversal
- Sum Root to Leaf Numbers
- Anagrams
- Unique Paths
- Unique Paths II
- Triangle
- Maximum Subarray(最大子串和問題)
- House Robber
- House Robber II
- Happy Number
- Interlaving String
- Minimum Path Sum
- Edit Distance
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock II
- Best Time to Buy and Sell Stock III
- Best Time to Buy and Sell Stock IV
- Decode Ways
- N-Queens
- N-Queens II
- Restore IP Addresses
- Combination Sum
- Combination Sum II
- Combination Sum III
- Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal
- Longest Consecutive Sequence
- Word Search
- Word Search II
- Word Ladder
- Spiral Matrix
- Jump Game
- Jump Game II
- Longest Substring Without Repeating Characters
- First Missing Positive
- Sort Colors
- Search for a Range
- First Bad Version
- Search Insert Position
- Wildcard Matching