leetcode刷题记录之1252

1252、奇数值单元格的数目

  给你一个 n 行 m 列的矩阵,最开始的时候,每个单元格中的值都是 0。
  另有一个索引数组 indices,indices[i] = [ri, ci] 中的 ri 和 ci 分别表示指定的行和列(从 0 开始编号)。
  你需要将每对 [ri, ci] 指定的行和列上的所有单元格的值加 1。
  请你在执行完所有 indices 指定的增量操作后,返回矩阵中 「奇数值单元格」 的数目。
示例 1:

输入:n = 2, m = 3, indices = [[0,1],[1,1]]
输出:6
解释:最开始的矩阵是 [[0,0,0],[0,0,0]]。
第一次增量操作后得到 [[1,2,1],[0,1,0]]。
最后的矩阵是 [[1,3,1],[1,3,1]],里面有 6 个奇数。

示例 2:

输入:n = 2, m = 2, indices = [[1,1],[0,0]]
输出:0
解释:最后的矩阵是 [[2,2],[2,2]],里面没有奇数。

提示:

  • 1 <= n <= 50
  • 1 <= m <= 50
  • 1 <= indices.length <= 100
  • 0 <= indices[i][0] < n
  • 0 <= indices[i][1] < m

难度:简单  题目地址:https://leetcode-cn.com/problems/cells-with-odd-values-in-a-matrix/

1、C语言代码:
int oddCells(int n, int m, int** indices, int indicesSize, int* indicesColSize){
    int res = 0;
    int i, j;
    int *a = (int*)malloc(n * sizeof(int));
    memset(a, 0, n*sizeof(int));
    int *b = (int*)malloc(m * sizeof(int));
    memset(b, 0, m*sizeof(int));
    for (i = 0; i < indicesSize; i++){
        a[indices[i][0]] = (a[indices[i][0]]+1)%2;
        b[indices[i][1]] = (b[indices[i][1]]+1)%2;
    }
    for (i = 0; i < n; i++){
        for (j = 0; j < m; j++){
            if (a[i] != b[j])
                res++;
        }
    }
    return res;
}

解释: 分别统计行和列出现的次数的奇偶性,然后行列进行重合,奇偶性相同为偶数,不同为奇数。

知识点回顾: 无。

2、Java代码:
class Solution {
    public int oddCells(int n, int m, int[][] indices) {
        int[] row=new int[n];
        int[] col=new int[m];       
        for(int i=0;i<indices.length;i++) {
         row[indices[i][0]]++;
         col[indices[i][1]]++;
        }        
        int ans=0;
        for(int i=0;i<n;i++)
         for(int j=0;j<m;j++) {
          if((row[i]+col[j])%2>0)
           ans++;
         }
        return ans;
    }
}

解释: 直接记一下行数和列数出现的次数即可。

知识点回顾: 无。

3、Python代码:
class Solution:
    def oddCells(self, n: int, m: int, indices: List[List[int]]) -> int:
        from collections import Counter
        rowCounter, colCounter = Counter(), Counter()
        for r, c in indices:
            rowCounter[r] += 1
            colCounter[c] += 1
        ans = 0
        for i in range(n):
            for j in range(m):
                if (rowCounter[i] + colCounter[j]) % 2:
                    ans += 1
        return ans

解释: 类似于C语言的求解思路。

知识点回顾: 无。

4、JavaScript代码:
/**
 * @param {number} n
 * @param {number} m
 * @param {number[][]} indices
 * @return {number}
 */
var oddCells = function(n, m, indices) {
    let row = new Array(n).fill(0)
    let col = new Array(m).fill(0)
    let sum = 0
    for(let i = 0 ; i < indices.length; i++){
        row[indices[i][0]] = row[indices[i][0]] ^ 1
    }
    let cnt_row = 0
    row.forEach((i)=>{cnt_row += i})
    let diff = n-cnt_row
    for(let i = 0 ; i < indices.length; i++){
        col[indices[i][1]] = col[indices[i][1]] ^ 1
    }
    col.forEach((i)=>{
        if(i == 1){
            sum+=diff
        }
        else{
            sum+=cnt_row
        }
    })
    return sum
};

解释: 先统计行的翻转情况,再统计列的翻转情况。 若某一列翻转了偶数次sum加上行反转次数,若某一列翻转了奇数次sum加上n行反转次数。

知识点回顾: 无。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值