162周的我也看了,就是写的不在咋地,补一下
有点二百五
1题
class Solution {
public:
int oddCells(int n, int m, vector<vector<int>>& indices) {
int sum=0;
vector<int> hang(n);
vector<int> lie(m);
for(int i=0;i<indices.size();i++){
hang[indices[i][0]]++;
lie[indices[i][1]]++;
}for(int a=0;a<n;a++){
for(int b=0;b<m;b++){
//cout<<hang[a]<<" "<<lie[b]<<endl;
if((hang[a]+lie[b])%2==1){
sum++;
}
}
}
return sum;
}
};
2题
class Solution {
public:
vector<vector<int>> reconstructMatrix(int upper, int lower, vector<int>& colsum) {
int n= colsum.size();
vector<int> v1(n);
vector<int> v2(n);
for(int i=0;i<n;i++){
if(colsum[i]==0){
v1[i]=0;
v2[i]=0;
}else if(colsum[i]==2){
v1[i]=1;
v2[i]=1;
upper--;
lower--;
}else if(colsum[i]==1){
if(upper>lower){
v1[i]=1;
v2[i]=0;
upper--;
}else{
v1[i]=0;
v2[i]=1;
lower--;
}
}
}
//cout<<upper<<" "<<lower<<endl;
vector<vector<int>> v;
if(lower<0||lower>0||upper>0||upper<0){
return v;
}
v.push_back(v1);
v.push_back(v2);
return v;
}
};
3题
class Solution {
public:
int numIslands(vector<vector<int>>& grid) {
int count = 10;
if(grid.size() == 0 || grid[0].size() == 0) return count;
int row = grid.size();
int col = grid[0].size();
for(int i = 0; i < row; i++){
for(int j = 0; j < col; j++){
if(grid[i][j] == 0){
count++;
maskAll(grid, i, j, row, col,count);
}
}
}
return count;
}
void maskAll(vector<vector<int>>& grid, int i, int j, int row, int col,int count){
if(grid[i][j] != 0){
return;
}
grid[i][j] = count;
if(i-1 >= 0){
maskAll(grid, i-1, j, row, col,count);
}
if(i+1 < row){
maskAll(grid, i+1, j, row, col,count);
}
if(j-1 >= 0){
maskAll(grid, i, j-1, row, col,count);
}
if(j+1 < col){
maskAll(grid, i, j+1, row, col,count);
}
return;
}
int closedIsland(vector<vector<int>>& grid) {
map<int,int>mp;
int sum=0;
int n=numIslands(grid);
for(int i=0;i<grid.size();i++){
mp[grid[i][0]]=1;
mp[grid[i][grid[0].size()-1]]=1;
//cout<<grid[i][grid[0].size()-1]<<endl;
}for(int i=0;i<grid[0].size();i++){
mp[grid[0][i]]=1;
mp[grid[grid.size()-1][i]]=1;
}
//cout<<n-10<<endl;
for(int i=11;i<=n;i++){
if(mp[i]==0){
sum++;
}
}return sum;
}
};