【leetcode】1252. Cells with Odd Values in a Matrix
题目如下:
Given
nandmwhich are the dimensions of a matrix initialized by zeros and given an arrayindiceswhereindices[i] = [ri, ci]. For each pair of[ri, ci]you have to increment all cells in rowriand columnciby 1.Return the number of cells with odd values in the matrix after applying the increment to all
indices.Example 1:
Input: n = 2, m = 3, indices = [[0,1],[1,1]]
Output: 6
Explanation: Initial matrix = [[0,0,0],[0,0,0]].
After applying first increment it becomes [[1,2,1],[0,1,0]].
The final matrix will be [[1,3,1],[1,3,1]] which contains 6 odd numbers.Example 2:
Input: n = 2, m = 2, indices = [[1,1],[0,0]]
Output: 0
Explanation: Final matrix = [[2,2],[2,2]]. There is no odd number in the final matrix.Constraints:
1 <= n <= 501 <= m <= 501 <= indices.length <= 1000 <= indices[i][0] < n0 <= indices[i][1] < m
解题思路:首先遍历indices,计算出每行每列分别做了几次+1的操作。然后再遍历matrix,根据matrix[i][j]求得对应的i行和j列分别做了几次+1,判断两者之和的奇偶性即可。
代码如下:
class Solution(object):
def oddCells(self, n, m, indices):
"""
:type n: int
:type m: int
:type indices: List[List[int]]
:rtype: int
"""
dic_row = {}
dic_column = {}
for (r,c) in indices:
dic_row[r] = dic_row.setdefault(r,0) + 1
dic_column[c] = dic_column.setdefault(c, 0) + 1
res = 0
for i in range(n):
for j in range(m):
count = dic_row.get(i,0) + dic_column.get(j,0)
if count % 2 == 1:res += 1
return res
【leetcode】1252. Cells with Odd Values in a Matrix的更多相关文章
- [LeetCode]1252. Cells with Odd Values in a Matrix
Given n and m which are the dimensions of a matrix initialized by zeros and given an array indices w ...
- 【LeetCode】378. Kth Smallest Element in a Sorted Matrix 解题报告(Python)
[LeetCode]378. Kth Smallest Element in a Sorted Matrix 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...
- 【Leetcode】378. Kth Smallest Element in a Sorted Matrix
Question: Given a n x n matrix where each of the rows and columns are sorted in ascending order, fin ...
- 【leetcode】378. Kth Smallest Element in a Sorted Matrix(TOP k 问题)
Given an n x n matrix where each of the rows and columns is sorted in ascending order, return the kt ...
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
- 【LeetCode】 454、四数之和 II
题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...
- 【leetcode】1021. Best Sightseeing Pair
题目如下: Given an array A of positive integers, A[i]represents the value of the i-th sightseeing spot, ...
- 【leetcode】998. Maximum Binary Tree II
题目如下: We are given the root node of a maximum tree: a tree where every node has a value greater than ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
随机推荐
- 吉首大学2019年程序设计竞赛(重现赛)-K(线段树)
题目链接:https://ac.nowcoder.com/acm/contest/992/K 题意:给一个大小为1e5的数组,由0 1组成,有两种操作,包括区间修改,将一段区间内的0换成1,1换成0; ...
- mysql一个SQL案例
需求 : 测试数据 ),start1 int,end1 int); ,); ,); ,); ,); ,); ,); 解决: 解决2: 解决代码 核心思想,把符合逻辑条件的行,构造相同分组 select ...
- Python中的逻辑运算符
- Android渐变色xml配置
这里渐变色: <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android=&quo ...
- 解决 mysql (10038)
1.授权 mysql>grant all privileges on *.* to 'root'@'%' identified by 'youpassword' with grant o ...
- python网络爬虫(5)BeautifulSoup的使用示范
创建并显示原始内容 其中的lxml第三方解释器加快解析速度 import bs4 from bs4 import BeautifulSoup html_str = """ ...
- spring基于注解的IoC以及IoC的案例
1.Spring中IoC的常用注解 1.1明确: (1)基于注解的配置和xml的配置要实现的功能都是一样的,都是要降低程序之间的耦合,只是配置的形式不一样 2.案例:使用xml方式和注解方式实现单表的 ...
- Vue中,过滤器的使用方法!
Vue.js允许自定义过滤器,可被用于一些常见的文本格式化.过滤器可以用在两个地方:双花括号插值和v-bind表达式.过滤器应该被添加在JavaScript表达式的尾部,由“管道”符号指示:(借官方的 ...
- mybatis查询返回的对象不为null,但是属性值为null
返回的对象不为null,但是属性值为null 代码如下: <resultMap id="BaseResultMap" type="com.trhui.ebook.d ...
- Support for the experimental syntax 'classProperties' isn't currently enabled
项目中使用高级语法报错, 报错信息 SyntaxError: E:\workdata\webpackVue\src\index.js: Support for the experimental syn ...

