对角线遍历 首先明白矩阵一共有几条对角线 总共有m+n-1条对角线这是数学问题。
然后依题意是从左上角到右下角的,对角线可分为奇对角线和偶对角线,奇对角线是从上往下走,偶对角线是从下往上走
class Solution {
public:
vector<int> findDiagonalOrder(vector<vector<int>>& mat) {
vector<int>result;
if(mat.empty())return result;
int m=mat.size();
int n=mat[0].size();
for(int i=0;i<m+n-1;i++)
{
if(i%2){//为奇数 则从上往下遍历
int x=i<n ? 0:i-n+1;//如果还处于第一行 那么就是0 如果比第一行多了 那么就会处于比列向量多的那几行
int y=i<n ? i:n-1; //如果处于第一行,那么y就是列向量,如果是最后一行 ,那么将是最后一行保持不变
while(x<m&&y>=0)
{
result.push_back(mat[x][y]);
x++;
y--;
}
}
else{
int x=i<m?i:m-1;
int y=i<m?0:i-m+1;
while(x>=0&&y<n)
{
result.push_back(mat[x][y]);
x--;
y++;
}
}
}
return result;
}
};
一个矩阵元素总和为行乘以列
所以看用一个for循环来求出每一行每一列所对应的值
class Solution {
public:
vector<vector<int>> matrixReshape(vector<vector<int>>& mat, int r, int c) {
int m=mat.size();
int n=mat[0].size();
int k=m*n;
if(k!=r*c)return mat;
vector<vector<int>>result(r,vector<int>(c));
for(int x=0;x<k;x++)
{
//x除以列向量与对列向量取余可以分别得到所在矩阵的行与列
result[x/c][x%c]=mat[x/n][x%n];
}
return result;
}
};
本题要求原地旋转,可以很明显看到 是将第一行变成了最后一列 最后一行变成了第一列
可以先将数组一个一个读取出来,再通过以右上角为起点,从新将值赋值给数组就完成了对比
class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
deque<int>queue;
for(int i=0;i<matrix.size();i++)
{
for(int j=0;j<matrix.size();j++)
{
queue.push_back(matrix[i][j]);
}
}
for(int i=matrix.size()-1;i>=0;i--)
{
for(int j=0;j<matrix.size();j++)
{
matrix[j][i]=queue.front();
queue.pop_front();
}
}
}
};
采用打标记的方式,对每一个一都为零,遇到不为零的将其置为0,并将其标定为false
class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
int m=matrix.size();
int n=matrix[0].size();
vector<vector<bool>>temp(m,vector<bool>(n,true));
for(int x=0;x<m*n;x++){
if(matrix[x/n][x%n]==0&&temp[x/n][x%n]==true)
{
for(int i=0;i<m;i++)
{
//将行向量都变为0
if(matrix[i][x%n]!=0)
{
temp[i][x%n]=false;
matrix[i][x%n]=0;
}
}
for(int j=0;j<n;j++)
{
//将列向量都变为0
if(matrix[x/n][j]!=0)
{
temp[x/n][j]=false;
matrix[x/n][j]=0;
}
}
}
}
}
};
采用标记的方式,先对每一个细胞周围的情况进行统计,看自己是否死亡还是活着,再次遍历数组来对数组进行更新。
重点理解规则,过去死的现在可能活就用2表示,过去活的现在死了,就用-1表示,如果过去活的现在还是活的就不懂
class Solution {
public:
void gameOfLife(vector<vector<int>>& board) {
int neighbors[3] = {0, 1, -1};
int rows = board.size();
int cols = board[0].size();
// 遍历面板每一个格子里的细胞
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
// 对于每一个细胞统计其八个相邻位置里的活细胞数量
int liveNeighbors = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (!(neighbors[i] == 0 && neighbors[j] == 0)) {
// 相邻位置的坐标
int r = (row + neighbors[i]);
int c = (col + neighbors[j]);
// 查看相邻的细胞是否是活细胞
if ((r < rows && r >= 0) && (c < cols && c >= 0) && (abs(board[r][c]) == 1)) {
liveNeighbors += 1;
}
}
}
}
// 规则 1 或规则 3
if ((board[row][col] == 1) && (liveNeighbors < 2 || liveNeighbors > 3)) {
// -1 代表这个细胞过去是活的现在死了
board[row][col] = -1;
}
// 规则 4
if (board[row][col] == 0 && liveNeighbors == 3) {
// 2 代表这个细胞过去是死的现在活了
board[row][col] = 2;
}
}
}
// 遍历 board 得到一次更新后的状态
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
if (board[row][col] > 0) {
board[row][col] = 1;
} else {
board[row][col] = 0;
}
}
}
}
};