本题是实现stack相关内容。
最早的时候写的代码如下,加粗部分的判断用==连接,
if(minstack.peek()==(stack.peek())
但是结果不正确,报告如下
Input:
["MinStack","push","push","push","push","pop","getMin","pop","getMin","pop","getMin"]
[[],[512],[-1024],[-1024],[512],[],[],[],[],[],[]]
Output:
["constructor","null","null","null","null","null","-1024","null","-1024","null","-1024"]
Expected:
["constructor","null","null","null","null","null","-1024","null","-1024","null","512"]
之后将加粗部分改为:
if(minstack.peek().equals(stack.peek()))
使用equals()连接,答案就是正确的了。
新人入码坑,求老师们指点一二这是为何?
另:这个结果报告是怎么看的,有点看不懂,是和二叉树有关的写法?求各位前辈指教!
谢谢!
附源码如下:
public class MinStack {
Stack<Integer> minstack = new Stack<Integer>();
Stack<Integer> stack = new Stack<Integer>();
public MinStack() {
}
public void push(int x) {
if(minstack.empty() || x <= minstack.peek())
minstack.push(x);
stack.push(x);
}
public void pop() {
**if(minstack.peek()==(stack.peek()))**
minstack.pop();
stack.pop();
}
public int top() {
return stack.peek();
}
public int getMin(){
return minstack.peek();
}
}
慕侠2389804
慕少森
达令说
Smart猫小萌
相关分类