当前位置:网站首页>剑指 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;
}
}
边栏推荐
- 构建增强现实移动应用程序的六款顶级工具
- KDD 2022 | 協同過濾中考慮錶征對齊和均勻性
- mysql远程连接
- 数据库是什么?数据库详细笔记!带你走进数据库~你想知道的这里都有!
- 以其他组件为代价的性能提升不是好提升
- MSYQL, redis, mongodb visual monitoring tool grafana
- What if the win11 policy service is disabled? Solution to disabling win11 policy service
- JVM (3) class loading
- 有了这4个安全测试工具,对软件安全测试say so easy!
- Oracle11.2.0.4-Rac集群hang分析记录
猜你喜欢
![[observation] softcom power liutianwen: embrace change and](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[observation] softcom power liutianwen: embrace change and "follow the trend" to become an "enabler" of China's digital economy

Win11 system component cannot be opened? Win11 system widget cannot be opened solution

【笔记】再笔记--边干边学Verilog HDL – 014

Canonical的工程师们正努力解决Firefox Snap的性能问题

What if the win11 policy service is disabled? Solution to disabling win11 policy service

NLP 类问题建模方案探索实践

npm ERR! fatal: early EOF npm ERR! fatal: index-pack failed

逻辑结构与物理结构

3-2主机发现-三层发现

Who took advantage of the chaos and looted in Tiktok Wenwan?
随机推荐
How important is it to make a silver K-line chart?
jfinal中如何使用过滤器监控Druid监听SQL执行?
建立自己的网站(12)
小米笔试真题一
3-3主機發現-四層發現
MBA-day26 数的概念与性质
Common knowledge of ECS security settings
【️爬虫必备->Scrapy框架从黑铁到王者️】初篇——万字博文详解(建议收藏)
Win11策略服务被禁用怎么办?Win11策略服务被禁用的解决方法
KDD 2022 | characterization alignment and uniformity are considered in collaborative filtering
软件测试逻辑覆盖相关理解
The concept and properties of mba-day26 number
誰在抖音文玩裏趁亂打劫?
虎符限币种提现 用户曲线出金即亏损
JVM (4) Bytecode Technology + Runtime Optimization
Classic illustration of K-line diagram (Collection Edition)
startService() 过程
Oracle11.2.0.4-Rac集群hang分析记录
Canonical的工程师们正努力解决Firefox Snap的性能问题
jfinal中如何使用过滤器监控Druid监听SQL执行?