1252 LeetCode 奇数值单元格的数目

题目描述:
LeetCode第1252题 奇数值单元格的数目
类型简单

思路:
为了节省时间
首先进行的统计,判断是奇数还是偶数
然后再进行列的统计,判断奇数还是偶数

代码如下:

class Solution {
public:
    int oddCells(int n, int m, vector<vector<int>>& indices) {
        int sum=0;
        map<int,int>row;
        map<int,int>col;
        for(int i=0;i<indices.size();i++){
            row[indices[i][0]]++;
            col[indices[i][1]]++;
        }
        for(auto p: row){
            int temp=p.second;
            if(temp%2!=0)
            sum++;
        }
        sum*=m;
        int even=0,odd=0;
        for(auto p:col){
            int temp=p.second;
            if(temp%2==0)
            even++;
            else odd++;
        }
        for(int i=0;i<n;i++){
            if(row[i]%2==0)
                sum+=odd;
            else sum-=odd;
        }
        return sum;
    }
};
发布了224 篇原创文章 · 获赞 0 · 访问量 3136

猜你喜欢

转载自blog.csdn.net/peachzy/article/details/104940377