Middle-题目115：71. Simplify Path

Given an absolute path for a file (Unix-style), simplify it.

For example,
path = “/home/”, => “/home”
path = “/a/./b/../../c/”, => “/c”

（1） 对多个/和.不作处理
（2） 对..则弹出栈顶元素，如果栈空（已经是根路径）则不处理
（3） 将两个/之间的子串压入堆栈

public class Solution {
public String simplifyPath(String path) {
String[] paths = path.split("/");
Stack<String> stack = new Stack<String>();
for (String str : paths) {
if (str.equals(""))
continue;
else if (str.equals("..")) {
if(!stack.isEmpty())
stack.pop();
continue;
}
else if (str.equals("."))
continue;
else
}
String result = "";
for(int i = 0;i<stack.size();i++) {
result=result+"/"+stack.get(i);
}
if(result=="")
result+="/";
return result;
}
}

12ms,beats 44.53%,众数12ms,18.84%

LeetCode --- 71. Simplify Path

2015-03-20 21:46:14

Leetcode 71. Simplify Path 简化路径 解题报告

2016-05-13 23:52:55

LeetCode71 Simplify Path java题解

2015-07-01 08:11:03

【leetcode】71. Simplify Path【java】

2016-12-19 11:20:26

leetcode No71. Simplify Path

2016-08-03 17:14:08

LeetCode 71. Simplify Path（简化路径）

2016-05-22 00:05:09

leetcode: 71. Simplify Path

2017-11-20 13:46:07

[Leetcode] 71. Simplify Path

2015-03-25 06:00:23

LeetCode||71. Simplify Path

2017-12-11 10:29:42

71. Simplify Path(Python3)

2018-01-28 09:41:38