当前位置:网站首页>力扣解法汇总1252-奇数值单元格的数目
力扣解法汇总1252-奇数值单元格的数目
2022-07-22 08:55:00 【失落夏天】
目录链接:
力扣编程题-解法汇总_分享+记录-CSDN博客
GitHub同步刷题项目:
https://github.com/September26/java-algorithms
原题链接:力扣
描述:
给你一个 m x n 的矩阵,最开始的时候,每个单元格中的值都是 0。
另有一个二维索引数组 indices,indices[i] = [ri, ci] 指向矩阵中的某个位置,其中 ri 和 ci 分别表示指定的行和列(从 0 开始编号)。
对 indices[i] 所指向的每个位置,应同时执行下述增量操作:
ri 行上的所有单元格,加 1 。
ci 列上的所有单元格,加 1 。
给你 m、n 和 indices 。请你在执行完所有 indices 指定的增量操作后,返回矩阵中 奇数值单元格 的数目。
示例 1:
输入:m = 2, n = 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:
输入:m = 2, n = 2, indices = [[1,1],[0,0]]
输出:0
解释:最后的矩阵是 [[2,2],[2,2]],里面没有奇数。
提示:
1 <= m, n <= 50
1 <= indices.length <= 100
0 <= ri < m
0 <= ci < n
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/cells-with-odd-values-in-a-matrix
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路:
* 解题思路: * 这题比较简单,按照规则求出最后的数组,然后计算奇数数量即可。
代码:
public class Solution1252 {
public int oddCells(int m, int n, int[][] indices) {
int[][] intss = new int[m][n];
for (int[] indice : indices) {
int i1 = indice[0];
int i2 = indice[1];
for (int i = 0; i < m; i++) {
intss[i][i2]++;
}
for (int i = 0; i < n; i++) {
intss[i1][i]++;
}
}
int result = 0;
for (int i = 0; i < intss.length; i++) {
for (int k = 0; k < intss[0].length; k++) {
if (intss[i][k] % 2 != 0) {
result++;
}
}
}
return result;
}
}
边栏推荐
- Gbase8s database union operator
- 力扣解法汇总745-前缀和后缀搜索
- 力扣解法汇总1200-最小绝对差
- 可重入函数
- Thread learning notes
- 2022-07-21:给定一个字符串str,和一个正数k, 你可以随意的划分str成多个子串, 目的是找到在某一种划分方案中,有尽可能多的回文子串,长度>=k,
- About the external entity key operation spinbox control in stemwin (fishing_2)
- 【Rust】我该用什么软件开发 Rust | 常用支持 Rust 的编辑器推荐
- 43. String multiplication
- DistSQL 深度解析:打造动态化的分布式数据库
猜你喜欢
【Rust】Rust 语言基础 | 学习语言都应该快速得出印象
Dokcer running Nacos container automatic exit problem
"Review of software engineering in Wuhan University of technology" Chapter 6 | coding specification
QT notes - operation EXECL
QT笔记——eventFilter事件过滤器
女嘉宾报名
互斥锁和信号量
2.树莓派系统备份
43. 字符串相乘
"Review of software engineering in Wuhan University of technology" Chapter III software requirements
随机推荐
主动降噪耳机排行榜10强,主动降噪耳机十大品牌
C # entry series (XXVII) -- Brief Analysis of LINQ
如何消除 MySQL表中的字段特别设置的字符集和排序规则?
力扣解法汇总648-单词替换
「武汉理工大学 软件工程复习」第一章 | 软件工程概述
Gbase8s database specifies the comparison order with set collation
Multi type low rate DDoS attack detection method based on hybrid deep learning
"Review of software engineering in Wuhan University of technology" Chapter III software requirements
SQL设计教学管理库
prosody相关概念了解。xmpp,jabber,bosh等
SQL多条件查询无法实现
解决 TypeScript 报错:A computed property name in an interface must refer to an expression whose type ...
Gbase8s database set database object mode statement
面向项目版本差异性的漏洞识别技术研究
小程序CMS动态处理数据之内容模型和内容集合的使用
三、泛型
Hblock revitalizes the enterprise storage market
C语言结构体初始化的四种方法
QT exe is only allowed to run a single
QT笔记——QTableWidget表格生成树,QTreeWidget树节点生成表格内容