当前位置:网站首页>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;
}
}
边栏推荐
- windbg
- 树的孩子兄弟表示法
- [leetcode] 13. linked list cycle · circular linked list
- [signal processing] weak signal detection in communication system based on the characteristics of high-order statistics with matlab code
- 【微信小程序开发(六)】绘制音乐播放器环形进度条
- Trivy [1] tool scanning application
- “29岁,普通功能测试,我是如何在一周内拿到5份Offer的?”
- One month's experience of joining Huawei OD
- @The function of valid (cascade verification) and the explanation of common constraint annotations
- JS event loop synchronous task, asynchronous task (micro task, macro task) problem analysis
猜你喜欢
![Trivy [1] tool scanning application](/img/b1/c05949f9379fcde658da64f3a0157a.png)
Trivy [1] tool scanning application

【自我成长网站收集】

Flutter God operation learning (full level introduction)

What "posture" does JD cloud have to promote industrial digitalization to climb to a "new level"?

Red hat official announced the new president and CEO! Paul Cormier, a key figure in transformation, is "retiring"

Newline required at end of file but not found.

Explanation of CNN circular training | pytorch series (XXII)

How to simply realize the function of menu dragging and sorting

unordered_map的hash function及hash bucket存储方式探索

数据中台夯实数据基础
随机推荐
【TA-霜狼_may-《百人计划》】图形3.7 移动端TP(D)R架构
Retainface use error: modulenotfounderror: no module named'rcnn.cyton.bbox'
[wechat applet development (V)] the interface is intelligently configured according to the official version of the experience version of the development version
How to authenticate Youxuan database client
A brief analysis of the differences between functional testing and non functional testing, recommended by Shanghai haokoubei software testing company
分层图解决的一些最短路问题
数据中台夯实数据基础
初识C语言 -- 操作符和关键字,#define,指针
Trivy [1] tool scanning application
Is the interface that can be seen everywhere in the program really useful? Is it really right?
openGauss源代码,用什么IDE工具管理、编辑、调试?
MySQL blocking monitoring script
Center Based 3D object detection and tracking (centerpoint) paper notes
[brother hero's July training] day 26: check the collection
Consolidate the data foundation in the data center
windbg
@The function of valid (cascade verification) and the explanation of common constraint annotations
分布式事务——Senta(一)
修改MySQL密码的四种方法(适合初学者)
What "posture" does JD cloud have to promote industrial digitalization to climb to a "new level"?