当前位置:网站首页>Day 19 of leetcode
Day 19 of leetcode
2022-07-28 02:55:00 【The sun is falling】
The number of odd cells
To give you one m x n Matrix , At the very beginning , The value in each cell is 0.
There is another two-dimensional index array indices,indices[i] = [ri, ci] Point to a position in the matrix , among ri and ci Respectively represent the specified row and column ( from 0 Numbered starting ).
Yes indices[i] Every position pointed , The following incremental operations should be performed at the same time :
- ri All cells on the row , Add 1 .
- ci All cells on the column , Add 1 .
Here you are. m、n and indices . Please finish executing all indices After the specified incremental operation , Returns odd cells in a matrix Number of .
analysis :
Because each operation will only increase the number of one row and one column 1, So you can use an array of rows rows And column array cols Record the number of times each row and column is increased . about indices Every pair of [ri, ci], take rows[ri] and cols[ci] The values of are increased respectively 1. After all operations are completed , We can calculate the position (x, y) The count of positions is rows[x]+cols[y]. ergodic matrix , You can get the number of all odd numbers .
class Solution {
public int oddCells(int m, int n, int[][] indices) {
int[] rows = new int[m];
int[] cols = new int[n];
for (int[] index : indices) {
rows[index[0]]++;
cols[index[1]]++;
}
int res = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (((rows[i] + cols[j]) & 1) != 0) {
res++;
}
}
}
return res;
}
}
Do not add, subtract, multiply or divide
Write a function , Find the sum of two integers , It is required that... Should not be used in the function body “+”、“-”、“*”、“/” Four operation symbols .
analysis :
Use bitwise operation .

class Solution {
public int add(int a, int b) {
while(b != 0) { // When carry is 0 Jump out when
int c = (a & b) << 1; // c = carry
a ^= b; // a = Non carry and
b = c; // b = carry
}
return a;
}
}Building a product array
Given an array A[0,1,…,n-1], Please build an array B[0,1,…,n-1], among B[i] Is the value of an array A In addition to subscript i The product of other elements , namely B[i]=A[0]×A[1]×…×A[i-1]×A[i+1]×…×A[n-1]. Division cannot be used .
analysis :
Our solution is simple ergodic calculation .
class Solution {
public int[] constructArr(int[] a) {
int[] res = new int[a.length];
for (int i = 0, cur = 1; i < a.length; i++) {
res[i] = cur; // Multiply the number on the left first ( Not including myself )
cur *= a[i];
}
for (int i = a.length - 1, cur = 1; i >= 0; i--) {
res[i] *= cur; // Multiply by the number on the right ( Not including myself )
cur *= a[i];
}
return res;
}
}Big guy's idea :
According to the main diagonal of the table ( All for 1 ), The table can be divided into Upper triangle and Lower triangle Two parts . Iteratively calculate the product of the lower triangle and the upper triangle respectively , that will do Don't use division You get results .

class Solution {
public int[] constructArr(int[] a) {
int len = a.length;
if(len == 0) return new int[0];
int[] b = new int[len];
b[0] = 1;
int tmp = 1;
for(int i = 1; i < len; i++) {
b[i] = b[i - 1] * a[i - 1];
}
for(int i = len - 2; i >= 0; i--) {
tmp *= a[i + 1];
b[i] *= tmp;
}
return b;
}
}
边栏推荐
- LETV responded that employees live an immortal life without internal problems and bosses; Apple refuses to store user icloud data in Russia; Dapr 1.8.0 release | geek headlines
- Design of edit memory path of edit box in Gui
- trivy【1】工具扫描运用
- JS event object offsetx/y clientx y pagex y
- “29岁,普通功能测试,我是如何在一周内拿到5份Offer的?”
- How to simply realize the function of menu dragging and sorting
- On the problem that sqli labs single quotation marks do not report errors
- 分布式 session 的4个解决方案,你觉得哪个最好?
- Commissioning experience of ROS
- JS 事件对象 offsetX/Y clientX Y PageX Y
猜你喜欢

Explanation of CNN circular training | pytorch series (XXII)

初识C语言 -- 结构体,分支和循环语句

CNN中的混淆矩阵 | PyTorch系列(二十三)

Chapter 3 business function development (batch export of market activities, Apache POI)

Docker高级篇-Docker容器内Redis集群配置

Usage of delegate
![[wechat applet development (VI)] draw the circular progress bar of the music player](/img/eb/9ce5d196970a6d6a887bf3e1d742ee.png)
[wechat applet development (VI)] draw the circular progress bar of the music player

Interpretation of cesium3dtilesets using customshader and examples of Omni effects

【信号处理】基于高阶统计量特征的通信系统中微弱信号检测附matlab代码

mysql 如图所示,现有表a,表b,需求为 通过projectcode关联a、b表,查出address不同的 idcardnum。
随机推荐
[TA frost wolf \u may - hundred people plan] Figure 3.7 TP (d) r architecture of mobile terminal
【英雄哥七月集训】第 27天:图
入职华为od一个月的感受
retainface使用报错:ModuleNotFoundError: No module named 'rcnn.cython.bbox'
别人发你的jar包你如何使用(如何使用别人发您的jar包)
[tutorial of using idea] shortcut key of idea
First knowledge of C language -- operators and keywords, define, pointer
Constant power wireless charging based on stm32
First knowledge of C language -- structure, branch and loop statements
JS event object offsetx/y clientx y pagex y
Center Based 3D object detection and tracking (centerpoint) paper notes
Usage of delegate
新基建助力智能化道路交通领域的转型发展
New infrastructure helps the transformation and development of intelligent road transportation
CNN循环训练的解释 | PyTorch系列(二十二)
第三章 队列
MySQL blocking monitoring script
Collision and rebound of objects in unity (learning)
selenium+pytest+allure综合练习
How to authenticate Youxuan database client