练习题
1 Question describing 832.Flipping an image
Given an n x n binary matrix image, flip the image horizontally, then invert it, and return the resulting image.
To flip an image horizontally means that each row of the image is reversed.
For example, flipping [1,1,0] horizontally results in [0,1,1].
To invert an image means that each 0 is replaced by 1, and each 1 is replaced by 0.
For example, inverting [0,1,1] results in [1,0,0].
class Solution {
public:
vector<vector<int>> flipAndInvertImage(vector<vector<int>>& image)
{
int rows=image.size();
int cols=image[0].size();
// for (int k = 0; k < rows; k++)
// {
// for(int i=0,j=cols-1;i<cols/2;i++,j--)
// swap(image[k][i],image[k][j]);
// }
for(int i=0;i<rows;i++)
{
reverse(image[i].begin(),image[i].end());
}
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
if(image[i][j])
{
image[i][j]=0;
}
else
{
image[i][j]=1;
}
}
}
return image;
}
};
//reverse函数的使用 A[i].begin() A[i].end()
//双指针法 小于一半或者小于等于右指针
2 Question Describing 867.Transpose Matrix
Given a 2D integer array matrix, return the transpose of matrix.
The transpose of a matrix is the matrix flipped over its main diagonal, switching the matrix's row and column indices.
class Solution {
public:
vector<vector<int>> transpose(vector<vector<int>>& matrix)
{
int rows=matrix.size();
int cols=matrix[0].size();
vector<vector<int>> res(cols,vector<int>(rows,0));
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
res[j][i]=matrix[i][j];
}
}
return res;
}
};
//新建二维向量
3 Question Describing Reshape the Matrix
In MATLAB, there is a handy function called reshape which can reshape an m x n matrix into a new one with a different size r x c keeping its original data.
You are given an m x n matrix mat and two integers r and c representing the number of rows and the number of columns of the wanted reshaped matrix.
The reshaped matrix should be filled with all the elements of the original matrix in the same row-traversing order as they were.
If the reshape operation with given parameters is possible and legal, output the new reshaped matrix; Otherwise, output the original matrix.
class Solution {
public:
vector<vector<int>> matrixReshape(vector<vector<int>>& mat, int r, int c)
{
int rows=mat.size();
int cols=mat[0].size();
if(c*r!=rows*cols)
{
return mat;
}
else
{
vector<vector<int>> temp(1,vector<int>(rows*cols));
vector<vector<int>> res(r,vector<int>(c,0));
int m=0;
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
temp[0][m]=mat[i][j];
m++;
}
}
int n=0;
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
res[i][j]=temp[0][n];
n++;
}
}
return res;
}
}
};
//ans[i / c][i % c] = nums[i / n][i % n]; i为1乘长度的矩阵下标
4 Question Describing 2022.Convert 1D Array Into 2D Array
You are given a 0-indexed 1-dimensional (1D) integer array original, and two integers, m and n. You are tasked with creating a 2-dimensional (2D) array with m rows and n columns using all the elements from original.
The elements from indices 0 to n - 1 (inclusive) of original should form the first row of the constructed 2D array, the elements from indices n to 2 * n - 1 (inclusive) should form the second row of the constructed 2D array, and so on.
Return an m x n 2D array constructed according to the above procedure, or an empty 2D array if it is impossible.
class Solution {
public:
vector<vector<int>> construct2DArray(vector<int>& original, int m, int n)
{
int rows=1;
int cols=original.size();
vector<vector<int>> v(m, vector<int>(n));
int total = original.size();
if(total != m*n)
{
return {};
}
else
{
for(int i = 0; i < total; i++)
v[i/n][i%n] = original[i];
return v;
}
}
};
5 Question Describing 1260.Shift 2D Grid
Given a 2D grid of size m x n and an integer k. You need to shift the grid k times.
In one shift operation:
Element at grid[i][j] moves to grid[i][j + 1].
Element at grid[i][n - 1] moves to grid[i + 1][0].
Element at grid[m - 1][n - 1] moves to grid[0][0].
Return the 2D grid after applying shift operation k times.
class Solution {
public:
vector<vector<int>> shiftGrid(vector<vector<int>>& grid, int k)
{
int rows=grid.size();
int cols=grid[0].size();
vector<vector<int>> res(rows,vector<int>(cols,0));
vector<int> temp(2*rows*cols,0);
int m=0;
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
temp[m]=grid[i][j];
temp[m+rows*cols]=grid[i][j];
m++;
}
}
int n=rows*cols-k%(rows*cols);
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
res[i][j]=temp[n];
n++;;
}
}
return res;
}
};
6 Question Describing 661.图片平滑器
包含整数的二维矩阵 M 表示一个图片的灰度。你需要设计一个平滑器来让每一个单元的灰度成为平均灰度 (向下舍入) ,平均灰度的计算是周围的8个单元和它本身的值求平均,如果周围的单元格不足八个,则尽可能多的利用它们
class Solution {
public:
vector<vector<int>> imageSmoother(vector<vector<int>>& img)
{
int rows=img.size();
int cols=img[0].size();
vector<vector<int>> ans(rows,vector<int>(cols));
int x[8] = {1, 1, 1, 0, 0,-1,-1,-1}; //8个方向
int y[8] = {1, 0,-1, 1,-1, 1, 0,-1};
for(int i=0 ; i<rows ; ++i)
{
for(int j=0 ; j<cols ; ++j)
{
int sum = img[i][j];//先加上自己的值
int num = 1;
for(int k=0 ; k<8 ; ++k)
{ //遍历周围8个方向
int a = i+x[k] , b = j+y[k];
if(a>=0 && a<rows && b>=0 && b<cols){ //如果没有越过数组边界
sum += img[a][b]; //计算总和
++num; //统计单元格数量
}
}
ans[i][j] = sum/num;
}
}
return ans;
}
};
//圈零法 常用于方阵
//判断九宫格内坐标是否合理,合理即计数
7 问题描述 1314.矩阵区域和
给你一个 m x n 的矩阵 mat 和一个整数 k ,请你返回一个矩阵 answer ,其中每个 answer[i][j] 是所有满足下述条件的元素 mat[r][c] 的和:
i - k <= r <= i + k,
j - k <= c <= j + k 且
(r, c) 在矩阵内。
class Solution {
public:
vector<vector<int>> matrixBlockSum(vector<vector<int>>& mat, int k)
{
int rows=mat.size();
int cols=mat[0].size();
vector<vector<int>> ans(rows,vector<int>(cols));
for(int i=0 ; i<rows ; ++i)
{
for(int j=0 ; j<cols ; ++j)
{
int sum=0;
for(int m=i-k;m<=i+k;m++)
{
for(int n=j-k;n<=j+k;n++)
{
if(m>=0&&m<rows&&n>=0&&n<cols)
{
sum+=mat[m][n];
}
}
}
ans[i][j]=sum;
}
}
return ans;
}
};
//属于二维前缀和题目,此方法有待改进