栈和队列1
队列
- 先入先出的数据结构
设计队列
class MyCircularQueue {
public:
int *queue;
int size;
int maxSize;
MyCircularQueue(int k) {
size=0;
maxSize=k;
queue=new int[k];
}
bool enQueue(int value) {
if(size>=maxSize)
return false;
queue[size++]=value;
return true;
}
bool deQueue() {
if(size==0)
return false;
for(int i=0;i<size-1;i++)
queue[i]=queue[i+1];
size--;
return true;
}
int Front() {
if(size==0)
return -1;
return queue[0];
}
int Rear() {
if(size==0)
return -1;
return queue[size-1];
}
bool isEmpty() {
return size==0;
}
bool isFull() {
return size==maxSize;
}
};
/**
* Your MyCircularQueue object will be instantiated and called as such:
* MyCircularQueue* obj = new MyCircularQueue(k);
* bool param_1 = obj->enQueue(value);
* bool param_2 = obj->deQueue();
* int param_3 = obj->Front();
* int param_4 = obj->Rear();
* bool param_5 = obj->isEmpty();
* bool param_6 = obj->isFull();
*/
队列的内置库
#include <iostream>
int main() {
// 1. Initialize a queue.
queue<int> q;
// 2. Push new element.
q.push(5);
q.push(13);
q.push(8);
q.push(6);
// 3. Check if queue is empty.
if (q.empty()) {
cout << "Queue is empty!" << endl;
return 0;
}
// 4. Pop an element.
q.pop();
// 5. Get the first element.
cout << "The first element is: " << q.front() << endl;
// 6. Get the last element.
cout << "The last element is: " << q.back() << endl;
// 7. Get the size of the queue.
cout << "The size is: " << q.size() << endl;
}
数据流中的移动平均值
给定一个整数数据流和一个窗口大小,根据该滑动窗口的大小,计算其所有整数的移动平均值。
实现 MovingAverage 类:
MovingAverage(int size) 用窗口大小 size 初始化对象。
double next(int val) 计算并返回数据流中最后 size 个值的移动平均值。
作者:力扣 (LeetCode)
链接:https://leetcode-cn.com/leetbook/read/queue-stack/k1msc/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
- 队列实现即可
class Solution {
public:
int index[4][2] = {
{-1,0}, {1, 0}, {0, -1}, {0, 1}};
void wallsAndGates(vector<vector<int>>& rooms) {
if (!rooms.size() || !rooms[0].size()) return;
for (int i = 0; i < rooms.size(); ++i) {
for (int j = 0; j < rooms[0].size(); ++j) {
if (rooms[i][j]) continue;
dfs(rooms, i, j, rooms[i][j]);
}
}
}
void dfs(vector<vector<int>>& rooms, int x, int y, int val) {
rooms[x][y] = val;
for (int i = 0; i < 4; ++i) {
int cx = x + index[i][0];
int cy = y + index[i][1];
if (cx>=0&&cx<rooms.size()&&cy>=0&&cy<rooms[0].size()&&rooms[x][y]+1<rooms[cx][cy]) {
dfs(rooms, cx, cy, rooms[x][y]+1);
}
}
}
};
作者:yuexiwen
链接:https://leetcode-cn.com/problems/walls-and-gates/solution/c-dfs-286-qiang-yu-men-by-yuexiwen/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
队列和广度优先搜索
墙与门
class Solution {
public:
//标记四个方向上下左右
int dx[4]={-1,1,0,0};
int dy[4]={0,0,-1,1};
void wallsAndGates(vector<vector<int>>& rooms) {
int line=rooms.size();
int row=rooms[0].size();
if (line==0 || row==0) return;
//从门开始,找门到点的距离,不断更新,直到找到点的最短距离
for(int i=0;i<line;i++)
{
for(int j=0;j<row;j++)
{
if(rooms[i][j]!=0) continue;
dfs(rooms,i,j,rooms[i][j]);
}
}
}
void dfs(vector<vector<int>>& rooms,int x,int y,int val)
{
int line=rooms.size();
int row=rooms[0].size();
//更新当前点的最短路径
rooms[x][y]=val;
//向四个方向寻找下一个点的最短路径
for(int i=0;i<4;i++)
{
//先走一步,到一个新的坐标
int cx=x+dx[i];
int cy=y+dy[i];
//看看这个新的坐标是不是能走
//看看这个新坐标保存的路径是不是经过(x,y)+1的路径长
//你就这么想,一开始去北京(cx,cy)要10000m,现在有个传送门离你1m,走这个传送门(x,y)可以不用走就到北京,那你的认知里去北京的最短路径就不再是10000m而是这1m,所以去北京的最短路径就更新为走去传送门的路径+传送门去北京的路径d(cy,cy)=d(x,y)+1,当然前提是你走到传送门后到达的位置没有超出地球的范围
if(cx>=0&&cx<line&&cy>=0&&cy<row&&rooms[x][y]+1<rooms[cx][cy])
{
//把(cx,cy)上的坐标的最短路径更新
dfs(rooms,cx,cy,rooms[x][y]+1);
}
}
}
};
岛屿数量
给你一个由 ‘1’(陆地)和 ‘0’(水)组成的的二维网格,请你计算网格中岛屿的数量。
岛屿总是被水包围,并且每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成。
此外,你可以假设该网格的四条边均被水包围。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/number-of-islands
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
class Solution {
public:
//定义向上下左右走
int dx[4]={-1,1,0,0};
int dy[4]={0,0,-1,1};
int numIslands(vector<vector<char>>& grid) {
int res=0;
for(int i=