当前位置:网站首页>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;
}
}
边栏推荐
- Special network technology virtual host PHP version setting
- [brother hero's July training] day 26: check the collection
- 特征值和特征向量
- Representation of children and brothers of trees
- P6118 [joi 2019 final] solution to the problem of Zhenzhou City
- 没法预测明天的涨跌
- 【软件测试】—— 自动化测试之unittest框架
- 数据中台建设(三):数据中台架构介绍
- 第三章 队列
- Leetcode judge whether palindrome number
猜你喜欢
![[TA frost wolf \u may hundred people plan] Figure 3.5 early-z and z-prepass](/img/85/2b6c9cf83340ee8bc01e85f77ec101.png)
[TA frost wolf \u may hundred people plan] Figure 3.5 early-z and z-prepass

Canvas 从入门到劝朋友放弃(图解版)

selenium+pytest+allure综合练习

When iPhone copies photos to the computer, the device connection often fails and the transmission is interrupted. Here's the way

超参数调整和实验-训练深度神经网络 | PyTorch系列(二十六)

别再用 offset 和 limit 分页了,性能太差!

Canonical Address

Pychart shortcut key for quickly modifying all the same names on the whole page

JS event object 2 e.charcode character code e.keycode key code box moves up, down, left and right

ROS的调试经验
随机推荐
【微信小程序开发(六)】绘制音乐播放器环形进度条
retainface使用报错:ModuleNotFoundError: No module named 'rcnn.cython.bbox'
超参数调整和实验-训练深度神经网络 | PyTorch系列(二十六)
怎么简单实现菜单拖拽排序的功能
【英雄哥七月集训】第 26天:并查集
Retainface use error: modulenotfounderror: no module named'rcnn.cyton.bbox'
Special network technology virtual host PHP version setting
【图像隐藏】基于DCT、DWT、LHA、LSB的数字图像信息隐藏系统含各类攻击和性能参数附matlab代码
树的孩子兄弟表示法
CSDN TOP1“一个处女座的程序猿“如何通过写作成为百万粉丝博主?
使用PyTorch的TensorBoard-可视化深度学习指标 | PyTorch系列(二十五)
【英雄哥七月集训】第 27天:图
How do gateways and chirpstacks in lorawan communicate? UDP? GRPC? MQTT?
[signal processing] weak signal detection in communication system based on the characteristics of high-order statistics with matlab code
unity中物体碰撞反弹(学习)
windbg
Is it safe to buy funds on Alipay? I want to make a fixed investment in the fund
入职华为od一个月的感受
Trivy [1] tool scanning application
【OpenGL】GLES20.glClear