if(stack2.empty()||stack2.top()>=op[i][1])判空放在||后面会报错
设计getMin功能的栈
http://www.nowcoder.com/questionTerminal/c623426af02d4c189f92f2a99647bd34
class Solution { public: /** * return a array which include all ans for op3 * @param op int整型vector<vector<>> operator * @return int整型vector */ vector<int> getMinStack(vector<vector<int> >& op) { // write code here int size=op.size(); stack<int>stack1; stack<int>stack2; vector<int>ret; for(int i=0;i<size;i++) { if(op[i][0]==1) { stack1.push(op[i][1]); if(stack2.empty()||stack2.top()>=op[i][1]) stack2.push(op[i][1]); } else if(op[i][0]==2) { if(!stack1.empty()) { if(stack2.top()==stack1.top()) stack2.pop(); stack1.pop(); } } else { ret.push_back(stack2.top()); } } return ret; } };