class Solution {
public boolean isValid(String s) {
int len=s.length();
if(len%2 ==1){
return false;
}
Stack<Character> stack = new Stack();
for( int i =0; i< len; i++){
if(s.charAt(i) =='(' || s.charAt(i) =='{' || s.charAt(i) =='['){
stack.push(s.charAt(i));
}else{
if(stack.isEmpty()){
return false;
}
}
if(s.charAt(i) ==')'){
char c = stack.pop();
if(c !='('){
return false;
}
}
if(s.charAt(i) =='}' ){
char c = stack.pop();
if(c !='{'){
return false;
}
}
if(s.charAt(i) ==']' ){
char c = stack.pop();
if(c !='['){
return false;
}
}
}
return stack.isEmpty();
}
}
class Solution {
public String simplifyPath(String path) {
String[] names = path.split("/");
Deque<String> stack = new ArrayDeque<String>();
for (String name : names) {
if ("..".equals(name)) {
if (!stack.isEmpty()) {
stack.pollLast();
}
} else if (name.length() > 0 && !".".equals(name)) {
stack.offerLast(name);
}
}
StringBuffer ans = new StringBuffer();
if (stack.isEmpty()) {
ans.append('/');
} else {
while (!stack.isEmpty()) {
ans.append('/');
ans.append(stack.pollFirst());
}
}
return ans.toString();
}
}
class MinStack {
private Deque<Integer> stack;
private List<Integer> list;
public MinStack() {
this.stack= new LinkedList();
this.list = new ArrayList();
stack.push(Integer.MAX_VALUE);
}
public void push(int val) {
list.add(val);
stack.push(Math.min(val,stack.peek()));
}
public void pop() {
list.remove(list.size()-1);
stack.pop();
}
public int top() {
return list.get(list.size()-1);
}
public int getMin() {
return stack.peek();
}
}
class Solution {
public int evalRPN(String[] tokens) {
Deque<Integer> stack = new LinkedList();
for(int i=0; i< tokens.length; i++){
if(!isCal(tokens[i])){
stack.push(Integer.valueOf(tokens[i]));
continue;
}else{
int num1 = stack.pop();
int num2 = stack.pop();
if(tokens[i].equals("+")){
stack.push(num1+num2);
}
if(tokens[i].equals("-")){
stack.push(num2-num1);
}
if(tokens[i].equals("*")){
stack.push(num1*num2);
}
if(tokens[i].equals("/")){
stack.push(num2/num1);
}
}
}
return stack.peek();
}
public boolean isCal(String str){
return str.equals("+") || str.equals("-") || str.equals("*") || str.equals("/");
}
}
class Solution {
public int calculate(String s) {
Deque<Integer> ops = new LinkedList<Integer>();
ops.push(1);
int sign =1;
int ret =0;
int n = s.length();
int i=0;
while(i <n ){
if(s.charAt(i)==' '){
i++;
}else if(s.charAt(i)=='+'){
sign=ops.peek();
i++;
}else if(s.charAt(i)=='-'){
sign =-ops.peek();
i++;
}else if(s.charAt(i)=='('){
ops.push(sign);
i++;
}else if(s.charAt(i)==')'){
ops.pop();
i++;
}else{
long num =0;
while(i<n && Character.isDigit(s.charAt(i))){
num = num*10 + s.charAt(i)-'0';
i++;
}
ret +=sign*num;
}
}
return ret;
}
}