当前位置:网站首页>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;
}
}
边栏推荐
- IO流:节点流和处理流详细归纳。
- Constant power wireless charging based on stm32
- [wechat applet development (VI)] draw the circular progress bar of the music player
- Redis aof日志持久化
- TypeScript(零) —— 简介、环境搭建、第一个实例
- First knowledge of C language -- operators and keywords, define, pointer
- Opengauss source code, what ide tools are used to manage, edit and debug?
- @Valid的作用(级联校验)以及常用约束注解的解释说明
- Four methods of modifying MySQL password (suitable for beginners)
- Selenium+pytest+allure comprehensive exercise
猜你喜欢

【信号去噪】基于卡尔曼滤波实现信号去噪附matlab代码

Commissioning experience of ROS

LoRaWAN中的网关和chirpstack到底如何通信的?UDP?GRPC?MQTT?

First knowledge of C language -- structure, branch and loop statements

Job 7.27 IO process

第二季度邮件安全报告:邮件攻击暴增4倍,利用知名品牌获取信任

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

入职华为od一个月的感受

Constant power wireless charging based on stm32

【 图像去雾】基于暗通道和非均值滤波实现图像去雾附matlab代码
随机推荐
2022.7.8 supplement of empty Luna
Typescript (zero) -- introduction, environment construction, first instance
数据中台建设(三):数据中台架构介绍
TypeScript(零) —— 简介、环境搭建、第一个实例
MySQL is shown in the figure. The existing tables a and B need to be associated with a and B tables through projectcode to find idcardnum with different addresses.
Chapter 3 business function development (batch export of market activities, Apache POI)
Notes for the fourth time of first knowing C language
Arm32 for remote debugging
Newline required at end of file but not found.
入职华为od一个月的感受
Why is there no unified quotation for third-party testing fees of software products?
Design of edit memory path of edit box in Gui
1313_pyserial的安装以及文档的生成
[self growth website collection]
What "posture" does JD cloud have to promote industrial digitalization to climb to a "new level"?
[image defogging] image defogging based on dark channel and non-mean filtering with matlab code
The virtual host website cannot access the self-test method
【TA-霜狼_may-《百人计划》】图形3.5 Early-z 和 Z-prepass
第二季度邮件安全报告:邮件攻击暴增4倍,利用知名品牌获取信任
Usage of delegate