【leetcode】1252. Cells with Odd Values in a Matrix
题目如下:
Given
n
andm
which are the dimensions of a matrix initialized by zeros and given an arrayindices
whereindices[i] = [ri, ci]
. For each pair of[ri, ci]
you have to increment all cells in rowri
and columnci
by 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 <= 50
1 <= m <= 50
1 <= indices.length <= 100
0 <= indices[i][0] < n
0 <= 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 ...
随机推荐
- Redhat7 安装 yum(换成免费版) 安装gcc
最近上Linux系统基础课程,要在虚拟机上编译运行程序,这时候就需要安装gcc,网上一搜,各种什么在线,离线安装,其中在线安装很方面,一个命令 yum install gcc 即可解决 可我这么输入后 ...
- Springboot---后台导出功能,easyExcel
Sprintboot+vuejs+easyExcel实现excel导出功能 一.背景 前段时间,有个需求,想要做一个excel导出功能,用来把查询到的数据进行导出.第一次做,所以搜了大量的资料,分为两 ...
- Oracle-DML- insert & update & delete
说明:语句中说到的“表”,以及表中有哪些“列”自行脑补......重要的是理解概率,能看懂语句代表的含义就OK~ DML-数据操作语句: 1. insert 新增 /*insert into 表名va ...
- Git及码云学习总结
前言 一.Git是一个版本管理工具软件. 二.windows 系统的使用: 1.git软件的安装:https://git-scm.com/downloads mac系统是自带的不用安装 windows ...
- springBoot中tomcat默认端口修改
springboot在启动tomcat的默认端口是8080,在实际开发中,应客户要求必须使用80端口. 研究springboot后发现有两种方式可以实现修改tomcat的端口 第一.直接修改appli ...
- P1816忠诚
这是一个区间查询最值的问题,用线段树来做. 建树的时候,这里不是求和,应该是e[k].w=min(e[k*2].w,e[k*2+1].w),所以这里要注意以下,其次是查询的时候,因为本题不用让我们修改 ...
- 2019牛客暑期多校训练营(第五场)G - subsequeue 1 (一题我真的不会的题)
layout: post title: 2019牛客暑期多校训练营(第五场)G - subsequeue 1 (一题我真的不会的题) author: "luowentaoaa" c ...
- redis 学习(11)-- redis pipeline
redis pipeline 什么是流水线(pipeline) 首先来看 redis 执行一次操作所需要的时间: 1 次时间 = 1 次网络时间 + 1次命令时间 执行 n 次就需要: n 次时间 = ...
- java中接口知识点大总结
接口的确很不好理解!!!!!那我来好好总结一下: 首先要理解接口是一个独立存在的,和类是不一样的东西,所以,直接用接口的定义是: 访问权限控制符 interface 接口名 [extends 接口列 ...
- 前端Ajax通过设置 timeout 参数,轮询后台API
因为我连接的数据库在台湾,相距较远,所以conn.Open()方法打开极慢.前端Ajax访问API时,API的数据还未返回,前端Ajax访问已经超时. 所以设置一个轮询,设置相隔多少秒之后进行一次查询 ...