week24
231 - 2的幂
class Solution {
public:
bool isPowerOfTwo(int n) {
// 这题 2的幂次 证明 二进制只能有一个1
if(n <= 0) return false;
return (n & (-n)) == n;
}
};232 - 用栈实现队列
class MyQueue {
public:
stack<int> stk, helper;
public:
/** Initialize your data structure here. */
MyQueue() = default;
/** Push element x to the back of queue. */
void push(int x) {
// 每次插入 都插入到头部
while(stk.size())
{
helper.push(stk.top());
stk.pop();
}
stk.push(x);
while(helper.size())
{
stk.push(helper.top());
helper.pop();
}
}
/** Removes the element from in front of queue and returns that element. */
int pop() {
int res = stk.top();
stk.pop();
return res;
}
/** Get the front element. */
int peek() {
return stk.top();
}
/** Returns whether the queue is empty. */
bool empty() {
return stk.empty();
}
};233 - 数字1的个数

234 - 回文链表
235 - 二叉搜索树的最近公共祖先
236 - 二叉树的最近公共祖先
237 - 删除链表中的节点
238 - 除自身以外数组的乘积
239 - 滑动窗口最大值
240 - 搜索二维矩阵II

Last updated
