当前位置:网站首页>Leetcode exercise - Sword finger offer 66 Build product array
Leetcode exercise - Sword finger offer 66 Build product array
2022-07-21 12:43:00 【SK_ Jaco】
1. Title Description
The finger of the sword Offer 66. 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 .
Example :
Input : [1,2,3,4,5]
Output : [120,60,40,30,24]
2. Problem solving ideas and codes
2.1 Their thinking
The question requires to get the product of the number of the array except the current bit , The first thought is to find the product of all numbers in the array , Then divide by the current bit , But if such a method encounters 0 It's going to be a mistake . Here you can use the idea of prefixes and arrays , First, traverse from left to right to get the product of the number on the left of the current bit , Then traverse from right to left to get the product of all bits on the right of the current number , Finally, multiply the product of the left and right sides of the current number to get the final result . With [1, 2, 3, 4, 5] As an example
First, we prepare an array to store the product of all numbers on the left of the number . Let the first be 1, Then it's convenient from left to right , Get the product to the left of the current number in turn
Then prepare an array to store the product of all the numbers on the right of the number . Set the last bit of the array to 1, Then traverse from right to left , Get the product of all numbers on the right of the current number in turn
Finally, multiply the two arrays by phase in order to get the final result
2.2 Code
class Solution {
public int[] constructArr(int[] a) {
int n = a.length;
int[] ans = new int[n];
if (n == 0) {
return ans;
}
int[] left = new int[n];
int[] right = new int[n];
left[0] = 1;
for (int i = 1; i < n; i++) {
left[i] = a[i - 1] * left[i - 1];
}
right[n - 1] = 1;
for (int i = n - 2; i >= 0; i--) {
right[i] = a[i + 1] * right[i + 1];
}
for (int i = 0; i < n; i++) {
ans[i] = left[i] * right[i];
}
return ans;
}
}
2.3 test result
Pass the test
3. summary
- Use the prefix and array idea to obtain the product of all numbers on the left and the product of all numbers on the right
- Multiply the left and right products in turn to get the final result
边栏推荐
猜你喜欢
你必须知道的4种 Redis 集群方案及优缺点对比
EL & JSTL: El: Department information query, jstl: Department query
Is CSDN like this now? No one reviews it?
Technology cloud report: cloud giant's midfield battle: PAAS and SaaS become key breaking points?
@Configuration和@Bean
Qt简单串口助手
Overseas app push (Part 1): differences between manufacturer channel and Google FCM channel
ACL and NAT
Week 5 - Neural Networks and Neural Language Models
IM即时通讯开发千万级并发长连接网关技术
随机推荐
SEO(Search Engine Optimization)搜索引擎优化
go mod创建项目
strcmp()与if语句
Segment tree - interval modification tree
Basic usage of async function and await expression in ES6
My creation anniversary (July 18, 2021 - July 18, 2022)
MySQL 入门基础(A)
[MySQL] get table information and all column names
go mod創建項目
常用测试用例设计方法之边界值分析法
Is it safe to open an account online? How to open an account? How much does it cost?
综述 | 实例分割研究
Common tools and test methods for interface testing (zero basis)
Some records of openvino installation
项目经理如何有效地进行项目工作量估算?
It's just a TCC distributed transaction. Is it so difficult?
@typescript-eslint/[email protected]
Is the online account opening of Oriental Fortune safe and formal
Problems of puts () for C language beginners
Four redis cluster schemes you must know and their advantages and disadvantages