-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode71.java
More file actions
29 lines (27 loc) · 814 Bytes
/
Copy pathleetcode71.java
File metadata and controls
29 lines (27 loc) · 814 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
26
27
28
29
package leetcode;
import java.util.Stack;
public class leetcode71 {
public static void main(String[] args) {
new leetcode71().simplifyPath("/a//b////c/d//././/..");
}
public String simplifyPath(String path) {
String [] aims=path.split("/");
Stack<String> stack=new Stack<>();
for (int i = 0; i <aims.length ; i++) {
if (aims[i].equals("")||aims[i].equals(".")){
continue;
}else if (aims[i].equals("..")){
if (!stack.isEmpty()){
stack.pop();
}
}else{
stack.push(aims[i]);
}
}
String res="";
while (!stack.isEmpty()){
res="/"+stack.pop()+res;
}
return res.length()==0 ?"/":res;
}
}