231.2的幂
题目链接
class Solution {
public:
bool isPowerOfTwo(int n) {
return n>0 && !(n&-n^n);
}
};
232.用栈实现队列
题目链接
class MyQueue {
public:
/** Initialize your data structure here. */
deque<int>qin;
deque<int>qout;
int nw = 1,pre = 0;
MyQueue() {
}
/** Push element x to the back of queue. */
void push(int x) {
qin.emplace_back(x);
}
/** Removes the element from in front of queue and returns that element. */
int pop() {
int ret;
if(qout.empty()){
while(!qin.empty()){
qout.emplace_back(qin.back());
qin.pop_back();
}
}
ret = qout.back();
qout.pop_back();
return ret;
}
/** Get the front element. */
int peek() {
if(qout.empty()){
while(!qin.empty()){
qout.emplace_back(qin.back());
qin.pop_back();
}
}
return qout.back();
}
/** Returns whether the queue is empty. */
bool empty() {
return qin.empty()&&qout.empty();
}
};