### 题目

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.

### 暴力定规则

1. 根据斜杠/切割成一个个独立的目录。比如/a/./b/../../c/切成[a],[.],[b],[..],[..],[c]
2. 开一个Stack（用Deque）。一个个往里放。
• 遇到.跳过。
• 遇到..不但跳过，前面如果有元素了，还要吐一个出来。
3. 最后给每个元素前面补偿一个/
4. 如果最后结果为空，直接返回/

#### 代码

public class Solution {
public String simplifyPath(String path) {
String[] segments = path.split("/");
Set<String> skip = new HashSet<>(Arrays.asList(new String[]{"",".",".."}));
Set<String> pop = new HashSet<>(Arrays.asList(new String[]{".."}));
for (String str : segments) {
if (pop.contains(str) && !stack.isEmpty()) { stack.removeFirst(); continue; }
if (!skip.contains(str)) { stack.offerFirst(str); }
}
String res = "";
while (!stack.isEmpty()) { res += ("/" + stack.pollLast()); }
return (res == "")? "/":res;
}
}