所以我在leetcode中做了一个问题.它是使用队列实现堆栈.如果我在下面提交此代码.它被接受了.
class Stack {
public:
queue<int> que;
// Push element x onto stack.
void push(int x) {
que.push(x);
for(int i=0;i<que.size()-1;i++){
que.push(que.front());
que.pop();
}
}
// Removes the element on top of the stack.
void pop() {
que.pop();
}
// Get the top element.
int top() {
return que.front();
}
// Return whether the stack is empty.
bool empty() {
return que.empty();
}
};
Run Code Online (Sandbox Code Playgroud)
但如果我只改for(int i=0;i<que.size()-1;++i)到for(int i=0;i<=que.size()-2;i++),我有时间限制的突破.最后执行的输入:push(1),empty().有人可以告诉我为什么??? 谢谢
queue::size()返回一个size_t,它基本上是一个无符号数.和负的无符号数转换为一个庞大的数字.
所以queue::size()-1- >巨大的数字(0xFFFFFFFF)