当前位置:网站首页>构建乘积数组
构建乘积数组
2022-07-20 01:19:00 【小刘学安卓】
知识点 数组
描述
给定一个数组 A[0,1,...,n-1] ,请构建一个数组 B[0,1,...,n-1] ,其中 B 的元素 B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1](除 A[i] 以外的全部元素的的乘积)。程序中不能使用除法。(注意:规定 B[0] = A[1] * A[2] * ... * A[n-1],B[n-1] = A[0] * A[1] * ... * A[n-2])
对于 A 长度为 1 的情况,B 无意义,故而无法构建,用例中不包括这种情况。
数据范围:1 \le n \le 10 \1≤n≤10 ,数组中元素满足 |val| \le 10 \∣val∣≤10
做这道题我只想出了暴力破解法。暴力破解法非常之简单。就是使用双层for循环把每个位置的左右两边的数都乘一遍。时间复杂度,太耗时了。
//暴力破解法
for (int i = 0; i < length; i++) {
int value = 1;
for (int j =0; j < i; j++) {
value *= A[j];
}
for (int k =i+1; k < length; k++) {
value *= A[k];
}
B[i] = value;
return B;
}
后面看了大神的解题思路,发现太吊炸天了。自己怎么没想到用两个数组把数组中每个数的左边乘积和右边乘积都单独计算和存储下来。感觉还是自己做题的时候不敢想,不敢大胆假设。
结合大神的解题思路,我自己尝试写了一下,思想是一样的,我写的更好理解,但是比大神的占用多一点空间。
下面代码思路还是很明了的,处理的时候小心一点临界点就好了。
int length = A.length;
//参考了大神的答案,自己尝试也写一遍
int [] B = new int[length]; // 用来存放A[i]位置处左边所有数的乘积
int [] C = new int [length]; // 用来存放A[i]位置处右边所有数的乘积
B[0] = 1; //A[0]左边没有数据
for (int i = 1; i <length; i++) {
B[i] = B[i - 1] * A[i-1];
}
C[length - 1] =1;
for (int i = length -2; i >=0; i--) {
//C[i]表示A[i]元素右边所有数的乘积
//所以C[i + 1]表示A[i + 1]元素右边所有数的乘积
//所以C[i] = C[i + 1] * A[i + 1]
C[i] = C[i + 1] * A[i + 1];
}
for (int i = 0; i < length; i++) {
A[i] = B[i] * C[i];
}
return A;
参考文章:
CodeSolution/剑指Offer51-构建乘积数组.md at main · Damaer/CodeSolution · GitHub
边栏推荐
- win11关闭Hyper-V
- Win11暂存文件夹是什么?Win11在线升级暂存文件夹在哪
- 【JVM 系列】JVM 中常见的垃圾回收器
- 我想在label标签上显示SQL数据库表里的记录数
- org.xml.sax.SAXParseException无法读取方案文档
- TestNG自动化测试框架详解
- CCTV news "Chengdu opens catering quota invoice by hand" news channel_ People's network
- Three principles CIOs should follow in order to successfully carry out digital transformation
- CCTV news "Shenzhen rent quota invoice by hand" news channel_ People's network
- Why has API strategy become a magic weapon for enterprises' digital transformation?
猜你喜欢
ADG备库可以进行数据泵导出吗?不可以
我的书《Oracle Database In-Memory架构与实践》出版了
Leetcode- number of occurrences of numbers in the array (single dog problem)
Win11新Bug任务栏图标不显示的解决方法
Web APIs DOM event delegation + comprehensive case
Web APIs DOM- 网页特效篇-滚动事件和加载事件
C#异步编程看这篇就够了
【731. 我的日程安排表 II】
Want to ensure software security at low cost? Five safety tasks worth considering
C # understand these 100 + lines of code, and you will really get started (Classic)
随机推荐
Net question and answer: is there the most efficient way to check large files in C?
校验码在线计算工具
CCTV news "Beijing opens accommodation quota invoice by hand" news channel_ People's network
我想在label标签上显示SQL数据库表里的记录数
零信任和SASE有什么不一样?答案其实并不重要
Recommend an open source mall
【731. 我的日程安排表 II】
C#异步编程看这篇就够了
【求解AX=b】
How to use parallel programming to improve task execution efficiency
CCTV news news news channel of Shenzhen catering manual tearing quota invoice_ People's network
Unlock high scores | eBay deepens user experience and optimizes large screen device applications
Popular understanding of tensor
redisconnectionfactory could not autowired
CCTV news news news channel of Chongqing catering manual tearing quota invoice_ People's network
STM32移植LVGL8.2
英伟达NX使用笔记
VMware 启动报错:Exception 0xc0000005和windwos11没有Hyper-V的解决方法
如何运用并行编程Parallel提升任务执行效率
CCTV news news "Suzhou restaurant manual tearing quota invoice" news channel_ People's network