832. Flipping an Image
Easy
552114FavoriteShare
Given a binary matrix A
, we want to 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]
.
对于动态二维数组a如果不事先定义她的大小,只能放的元素是一个动态数组,a.push_back(vector<int>b),而不能放入一个int类型,a[i].push_back(1),不可行,除非是 vector<vector<int>>a(3)事先定义她的大小了,是可以的
vector<vector<int>> flipAndInvertImage(vector<vector<int>>& A) {
vector<vector<int>>a;
for(int i=0;i<A.size();i++){
vector<int>temp;
for(int j=A[i].size()-1;j>=0;j--){
if (A[i][j]==0) {
temp.push_back(1);
}
else{
temp.push_back(0);
}
}
a.push_back(temp);
}
return a;
}