当前位置:网站首页>剑指 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;
}
}
边栏推荐
- 使用 OpenCV 的基于标记的增强现实
- Classic illustration of K-line diagram (Collection Edition)
- 凌云出海记 | 文华在线&华为云:打造非洲智慧教学新方案
- 数据库是什么?数据库详细笔记!带你走进数据库~你想知道的这里都有!
- Mba-day19 if P then q contradictory relation P and not Q
- Win11系统小组件打不开?Win11系统小组件无法打开解决方法
- 福昕软件受邀亮相2022先进制造业数智发展论坛
- From CIO to Consultant: the transformation of it leaders
- How is the combination of convolution and transformer optimal?
- 2022年理财利率都降了,那该如何选择理财产品?
猜你喜欢

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

3-3主机发现-四层发现

After CDN is added to the website, the Font Icon reports an error access control allow origin

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

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

做白银k线图有多重要?

Exploration and practice of NLP problem modeling scheme

开发者任务中心上线!千元豪礼送不停!

C#_摄像头图像转换为Bitmap格式及绘制十字线

Win11系统小组件打不开?Win11系统小组件无法打开解决方法
随机推荐
Connaissance générale des paramètres de sécurité du serveur Cloud
Nacos problem
MBA-day26 数的概念与性质
What if the win11 policy service is disabled? Solution to disabling win11 policy service
npm ERR! fatal: early EOF npm ERR! fatal: index-pack failed
Test method learning
乐鑫面试流程
Installation and configuration of MariaDB
Shell bash script note: there must be no other irrelevant characters after the escape character \ at the end of a single line (multi line command)
做白银k线图有多重要?
PHP Laravel 使用 aws 负载均衡器的 ip 错误问题
罗清启:高端家电已成红海?卡萨帝率先破局
jfinal中如何使用过滤器监控Druid监听SQL执行?
JVM (4) bytecode technology + runtime optimization
docker compose 部署Flask项目并构建redis服务
npm ERR! fatal: early EOF npm ERR! fatal: index-pack failed
Sophomore majoring in software engineering, the previous learning situation is not very good. How to plan the follow-up development route
Automatically obtain local connection and network address modification
JVM(4) 字节码技术+运行期优化
【观察】软通动力刘天文:拥抱变化“顺势而为”,做中国数字经济“使能者”...