"LeetCode Zero Basic Guide" (Lesson 8) Two-dimensional array

1351. Counting Negative Numbers in Ordered Matrices

Because the data is not increasing, the numbers after the first negative number in each row must be all negative numbers.

class Solution {
    
    
public:
    int countNegatives(vector<vector<int>>& grid) {
    
    
        int ans=0;
        for(int i=0;i<grid.size();i++) {
    
    
            int l=0,r=grid[i].size()-1;
            while(l<r) {
    
    
                int mid=l+r>>1;
                if(grid[i][mid]<0) r=mid;
                else l=mid+1;
            }
            if(grid[i][l]<0) ans+=grid[i].size()-l;
        }
        return ans;
    }
};

1572. Sum of Matrix Diagonal Elements

Iterate over all the lines, and then add the two numbers of the diagonal position of each line. Special judgment if this number is in the middle (the junction of the two diagonals) minus one

class Solution {
    
    
public:
    int diagonalSum(vector<vector<int>>& mat) {
    
    
        int sum=0;
        for(int i=0;i<mat.size();i++) {
    
    
            sum+=mat[i][i]+mat[i][mat[0].size()-i-1];
            if(mat[0].size()-i-1==i) sum-=mat[i][i];
        }
        return sum;
    }
};

1672. Total assets of the wealthiest customers

Summing up each row to update the maximum value

class Solution {
    
    
public:
    int maximumWealth(vector<vector<int>>& accounts) {
    
    
        int res=0;
        for(auto t : accounts) {
    
    
            int cnt=0;
            for(int j=0;j<t.size();j++) {
    
    
                cnt+=t[j];
            }
            res=max(res,cnt);
        }
        return res;
    }
};

766. Toeplitz Matrix

According to the definition of Toeplitz matrix , we only need to judge whether each element is equal to the previous element of the previous row.

class Solution {
    
    
public:
    bool isToeplitzMatrix(vector<vector<int>>& matrix) {
    
    
        for(int i=1;i<matrix.size();i++) {
    
    
            for(int j=1;j<matrix[0].size();j++) {
    
    
                if(matrix[i][j]!=matrix[i-1][j-1]) return false;
            }
        }
        return true;
    }
};

1380. Lucky Numbers in Matrix

Find the meaning of the simulation questions by row and column

class Solution {
    
    
public:
    vector<int> luckyNumbers (vector<vector<int>>& matrix) {
    
    
        vector<int>ans;
        for(int i=0;i<matrix.size();i++) {
    
    
            int flag=0,st=0x3f3f3f3f,ed=0;
            for(int j=0;j<matrix[i].size();j++) {
    
    
                if(matrix[i][j]<st) {
    
    
                    st=matrix[i][j],flag=j;
                }
            }
            for(int j=0;j<matrix.size();j++) {
    
    
                if(matrix[j][flag]>ed) {
    
    
                    ed=matrix[j][flag];
                }
            }
            if(ed==st) ans.push_back(ed);
        }
        return ans;
    }
};

1582. Special Positions in Binary Matrix

simulation

class Solution {
    
    
public:
    int numSpecial(vector<vector<int>>& mat) {
    
    
        int res=0;
        for(auto t:mat) {
    
    
            int sum=0;
            int flag;
            for(int j=0;j<t.size();j++) {
    
    
                sum+=t[j];
                if(t[j]==1) flag=j;
            }
            if(sum==1) {
    
    
                int cnt=0;
                for(int j=0;j<mat.size();j++) {
    
    
                    cnt+=mat[j][flag];
                }
                if(cnt==1) res++;
            }
        }
        return res;
    }
};

463. Perimeter of the Island

dfs, add one to the answer when it is found to be surrounded by water or a boundary

typedef pair<int,int>PII;

int dx[]={
    
    0,-1,0,1};
int dy[]={
    
    1,0,-1,0};

class Solution {
    
    
public:
    int islandPerimeter(vector<vector<int>>& grid) {
    
    
        PII st;
        int ans=0;
        queue<PII>qu;
        for(int i=0;i<grid.size();i++) {
    
    
            for(int j=0;j<grid[0].size();j++) {
    
    
                if(grid[i][j]==1) {
    
    
                    st={
    
    i,j};
                    grid[i][j]=-1;
                    goto m;
                }
             }
        }
        m:;
        qu.push(st);
        while(qu.size()) {
    
    
            auto t=qu.front();
            qu.pop();
            int cnt=0;
            for(int i=0;i<4;i++) {
    
    
                int x=dx[i]+t.first;
                int y=dy[i]+t.second;
                if(x>=0&&x<grid.size()&&y>=0&&y<grid[0].size()&&grid[x][y]==1) {
    
    
                    grid[x][y]=-1;
                    qu.push({
    
    x,y});
                }
                else if((x>=0&&x<grid.size()&&y>=0&&y<grid[0].size()&&grid[x][y]==0)||x<0||x>=grid.size()||y<0||y>=grid[0].size()) cnt++;
            }
            ans+=cnt;
        }
        return ans;
    }
};

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324104494&siteId=291194637