class MyStack {
List<Integer> list = new ArrayList<>();
public MyStack() {
}
public void push(int x) {
list.add(x);
}
public int pop() {
return list.remove(list.size()-1);
}
public int top() {
return list.get(list.size()-1);
}
public boolean empty() {
return list.isEmpty();
}
}
/**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* boolean param_4 = obj.empty();
*/
题解:
用ArrrayList模拟栈,主要考察ArrayList的API用法。
数据结构:
ArrayList
用到方法:
list.add(int i);
list.remove(index);
list.get(index);
list.isEmpty();