当前位置:网站首页>剑指 Offer 66. 构建乘积数组
剑指 Offer 66. 构建乘积数组
2022-06-29 19:37:00 【Yake1965】
剑指 Offer 66. 构建乘积数组
class Solution {
public int[] constructArr(int[] a) {
int n = a.length;
if(n == 0) return new int[0];
int[] ans = new int[n]; // ans[i] 表示索引 i 左侧所有元素的乘积
ans[0] = 1; // 因为索引为 '0' 的元素左侧没有元素,所以 ans[0] = 1
for(int i = 1; i < n; i ++)
ans[i] = a[i-1] * ans[i-1];
// R 为右侧所有元素的乘积,刚开始右边没有元素,所以 R = 1
for(int i = n - 1, r = 1; i >= 0; i--){
ans[i] = ans[i] * r; // 对于索引 i,左边的乘积为 ans[i],右边的乘积为 R
r *= a[i];
}
return ans;
}
}
边栏推荐
- npm ERR! fatal: early EOF npm ERR! fatal: index-pack failed
- How important is it to make a silver K-line chart?
- npm ERR! fatal: early EOF npm ERR! fatal: index-pack failed
- Luoqingqi: has high-end household appliances become a red sea? Casati took the lead in breaking the game
- Kdd 2022 | prise en compte de l'alignement et de l'uniformité des représentations dans le Filtrage collaboratif
- 7.取消与关闭
- Automatically obtain local connection and network address modification
- MBA-day26 数的概念与性质
- MBA-day19 如果p则q矛盾关系p 且非q
- JVM(4) 字節碼技術+運行期優化
猜你喜欢

高能直播,大咖云集!邀你共启BizDevOps探索之路。

【软件测试】01 -- 软件生命周期、软件开发模型

JVM(4) 字節碼技術+運行期優化

One hour to build a sample scenario sound network to release lingfalcon Internet of things cloud platform

Where is the win11 installation permission set? Win11 installation permission setting method

建立自己的网站(12)

DAO 中存在的不足和优化方案

使用 OpenCV 的基于标记的增强现实

QC协议+华为FCP+三星AFC快充取电5V9V芯片FS2601应用

STM32CubeMX 学习(6)外部中断实验
随机推荐
【笔记】再笔记--边干边学Verilog HDL – 014
Game Maker 基金会呈献:归属之谷
mysql远程连接
3-3主機發現-四層發現
Have you mastered all the testing methods of technology to ensure quality and software testing?
What if the win11 policy service is disabled? Solution to disabling win11 policy service
云服务器的安全设置常识
@SneakyThrows注解
From CIO to Consultant: the transformation of it leaders
ovirt数据库修改删除节点
JVM(4) 字節碼技術+運行期優化
Sophomore majoring in software engineering, the previous learning situation is not very good. How to plan the follow-up development route
构建增强现实移动应用程序的六款顶级工具
QC协议+华为FCP+三星AFC快充取电5V9V芯片FS2601应用
swift可选值总结
Docker compose deploy the flask project and build the redis service
Canonical engineers are trying to solve the performance problem of Firefox snap
一小时构建示例场景 声网发布灵隼物联网云平台
4-2端口Banner信息获取
Win11系统频繁断网怎么办?Win11网络不稳定的解决方法