【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 ...
随机推荐
- Sink - 汇聚点
!!!1.Logger Sink 记录INFO级别的日志,通常用于调试. 属性说明: !channel – !type – The component type name, needs to ...
- window启动目录
启动文件目录 C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
- Windows系统中CMD wmic查看硬盘、内存、CPU、BIOS、网卡等信息。(附带脚本)
目录 Windows系统中CMD wmic查看硬盘.内存.CPU.BIOS.网卡等信息. 查看内存相关 查看内存主板数量(两条命令均可) 查看物理内存 查看逻辑内存 查看缓存 查看虚拟内存 查看网络相 ...
- Employee Free Time
We are given a list schedule of employees, which represents the working time for each employee. Each ...
- python list pop()方法
#pop()用于移除列表中的一个元素(默认是最后一个元素,并且返回该元素的值) list1=['Google','Runoob','Taobao'] list_pop=list1.pop() prin ...
- redis缓存雪崩
缓存雪崩 缓存雪崩,是指在某一个时间段,缓存集中过期失效. 产生雪崩的原因之一,比如在写本文的时候,马上就要到双十二零点,很快就会迎来一波抢购,这波商品时间比较集中的放入了缓存,假设缓存一个小时.那么 ...
- java开发性能调优
从总体上来看,对于大型网站,比如门户网站,在面对大量用户访问.高并发请求方面,基本的解决方案集中在这样几个环节:1.首先需要解决网络带宽和Web请求的高并发,需要合理的加大服务器和带宽的投入,并且需要 ...
- jsp页面中使用 splitfn:split注意事项
我们有一个 字段存储内容是 xxxx意见~~@~~是 在页面上需要分开显示,格式为 xxx意见 是 使用 ${fn:split(comments, '~~@~~')[1]} 来分割是发现出现@符文字 ...
- 如何同步多个 git 远程仓库
请看 -> 如何同步多个 git 远程仓库
- Git 一般性操作
git全局设定 git config --global user.name “码云账号” git config --global user.email “码云注册邮箱” git 定位文件夹cd进入到需 ...

