【每日一题 春季4】3174. 旋转 & 73. 矩阵置零

Day4 AcWing 3174. 旋转
Day4 LeetCode 73. 矩阵置零

思路

  1. 数组
  2. 遍历

代码

3174. 旋转
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>
using namespace std;

int m,n;
int a[110][110];

int main()
{
cin>>m>>n;
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
cin>>a[i][j];
for(int j=0;j<n;j++)
{
for(int i=m-1;i>=0;i--)
cout<<a[i][j]<<" ";
cout<<endl;
}
}
73. 矩阵置零
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
unordered_set<int> row;
unordered_set<int> col;
int m=matrix.size();
int n=matrix[0].size();
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
{
if(matrix[i][j]==0)
{
row.insert(i);
col.insert(j);
}
}
for(auto i:row)
{
for(int j=0;j<n;j++) matrix[i][j]=0;
}
for(auto j:col)
{
for(int i=0;i<m;i++) matrix[i][j]=0;
}
}
};