-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stacks.java
105 lines (84 loc) · 2.47 KB
/
Stacks.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import java.util.Stack;
public class Stacks {
public static String simplifyPath(String path) {
Stack<String> s = new Stack<>();
String[] ars = path.split("/");
for (int i = 0; i < ars.length; i++) {
if(ars[i].isEmpty()) continue;
if (ars[i].equals("."))
continue;
if (ars[i].equals("..")) {
if (s.empty()) {
continue;
} else {
s.pop();
}
} else {
s.push(ars[i]);
}
}
Stack<String> st1 = new Stack<>();
while (!s.empty()) {
st1.push(s.pop());
}
StringBuilder str = new StringBuilder("");
while (!st1.empty()) {
String newS = st1.pop();
if (!newS.isEmpty()) {
str.append("/");
str.append(newS);
}
}
if (str.toString().isEmpty()){ return "/";}
return str.toString();
}
public class MinStack {
public void push(int val) {
}
public void pop() {
}
public int top() {
return 0;
}
public int getMin() {
return 0;
}
public static void main(String[] args) {
}
}
public int evalRPN(String[] tokens) {
Stack<Integer> s = new Stack<>();
for(int i = 0 ; i < tokens.length ; i++){
if (tokens[i].equals("+")){
int num1 = s.pop();
int num2 = s.pop();
s.push(num2 + num1);
continue;
}
if (tokens[i].equals("-")){
int num1 = s.pop();
int num2 = s.pop();
s.push(num2 - num1);
continue;
}
if (tokens[i].equals("*")){
int num1 = s.pop();
int num2 = s.pop();
s.push(num2 * num1);
continue;
}
if (tokens[i].equals("/")){
int num1 = s.pop();
int num2 = s.pop();
s.push(num2 / num1);
continue;
}
s.push(Integer.parseInt(tokens[i]));
}
return s.peek();
}
public static void main(String[] args) {
String s = "/home/";
System.out.println(simplifyPath(s));
}
}