-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSimplifyPath.java
More file actions
43 lines (38 loc) · 1.29 KB
/
SimplifyPath.java
File metadata and controls
43 lines (38 loc) · 1.29 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package leetcode.stack;
import java.util.Stack;
public class SimplifyPath {
public String simplifyPath(String path) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
Stack<String> stack = new Stack<String>();
int len = path.length();
for(int i = 0; i < len; i++){
while(i < len && path.charAt(i) == '/') i++;
if(i == len)
break;
int start = i;
while(i < len && path.charAt(i) != '/') i++;
int end = i;
String element = path.substring(start, end);
if(element.equals("..")){
if(!stack.isEmpty())
stack.pop();
} else if(!element.equals(".")){
stack.push(element);
}
}
if(stack.size() == 0) return "/";
String output = "";
for(int i = 0; i < stack.size(); i++)
output += "/" + stack.get(i);
return output;
}
public static void main(String[] args) {
SimplifyPath sp = new SimplifyPath();
System.out.println(sp.simplifyPath("/.."));
int i = 1;
long w = 0;
if(w == (long)i)
System.out.println("s");
}
}