forked from yuduozhou/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSimplifyPath.java
More file actions
25 lines (25 loc) · 785 Bytes
/
SimplifyPath.java
File metadata and controls
25 lines (25 loc) · 785 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class Solution {
public String simplifyPath(String path) {
// Start typing your Java solution below
// DO NOT write main() function
String result = "/";
String[] stubs = path.split("/+");
ArrayList<String> paths = new ArrayList<String>();
for (String s : stubs){
if(s.equals("..")){
if(paths.size() > 0){
paths.remove(paths.size() - 1);
}
}
else if (!s.equals(".") && !s.equals("")){
paths.add(s);
}
}
for (String s : paths){
result += s + "/";
}
if (result.length() > 1)
result = result.substring(0, result.length() - 1);
return result;
}
}