-
Notifications
You must be signed in to change notification settings - Fork 288
/
AC_simulation_n.java
44 lines (39 loc) · 1.12 KB
/
AC_simulation_n.java
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
44
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_simulation_n.java
* Create Date: 2015-02-04 09:59:37
* Descripton:
*/
import java.util.Scanner;
import java.util.Stack;
public class Solution {
public String simplifyPath(String path) {
Stack<String> res = new Stack<String>();
String[] ps = path.split("/");
for (String a : ps) {
if (a.equals("..")) {
if (!res.empty()) {
res.pop();
}
} else if (a.equals(".") || a.equals("")) {
continue;
} else {
res.push(a);
}
}
if (res.empty()) {
return "/";
} else {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < res.size(); ++i)
sb.append("/" + res.get(i));
return sb.toString();
}
}
// debug
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
Solution s = new Solution();
System.out.println(s.simplifyPath("/home//fsdf///dds//.././/"));
}
}