字符串的雷踩到炸成一等残废棒球比赛
class Solution {
public int calPoints(String[] ops) {
int length=ops.length;
int[] point=new int[2];
Stack<Integer> stack=new Stack<>();
for(int i=0;i<length;i++){
if(ops[i].equals("C")){stack.pop();}
else if(ops[i].equals("D")){point[0]=stack.peek()*2;stack.push(point[0]);}
else if(ops[i].equals("+")){point[0]=stack.pop();point[1]=stack.peek()+point[0];stack.push(point[0]);stack.push(point[1]);}
else{point[0]=Integer.valueOf(ops[i]);stack.push(point[0]);}
}
point[0]=0;
while(!stack.empty()){point[0]+=stack.pop();}
return point[0];
}
}
arraylist用size,数组用length
string转int:Integer.valueOf(str)
stack不能用索引找,只能pop peek,查看是否空要用stack.empty()
——————————————————————————————
对于LinkedList:
add() 等价于 linkLast() // 相当于在尾部插入
pollLast() // 移除尾部的元素
push() 等价于 addFirst() // 在头部插入
——————————————————————————————