[leetcode]238. Product of Array Except Self除了自身以外的数组元素乘积
2024-08-25 18:04:31
Given an array nums
of n integers where n > 1, return an array output
such that output[i]
is equal to the product of all the elements of nums
except nums[i]
.
Example:
Input:[1,2,3,4]
Output:[24,12,8,6]
Note: Please solve it without division and in O(n).
思路
1. from left to right, save each item's left side product
2. from right to left, maintain a variable temp to track each item's right side product, then fill product (left * right) into result
代码
class Solution {
public int[] productExceptSelf(int[] nums) {
int[]dp = new int[nums.length]; dp[0] = 1; // left to right
for( int i = 1; i< nums.length; i++){
dp[i] = dp[i-1] * nums[i-1];
}
// right to left
int temp = 1;
for( int i = nums.length-1; i>=0; i--){
dp[i] = dp[i] *temp;
temp = temp*nums[i];
}
return dp; }
}
最新文章
- 9.JAVA中的正则表达式
- urlscan使用详解
- 你一定能用的上的iOS第三方库
- 2048-AI程序算法分析
- 利用rlwrap配置linux下oracle sqlplus 历史记录回调
- 区分jquery中的offset和position
- Fedora 21 安装QQ国际版
- Hash表——The Hash table
- 9.XML文件解析
- JS——基础知识(三)
- codeforce vk cup2017
- Python安装与环境变量
- delphi 多线程之System.TMonitor
- python问题:AttributeError: &#39;module&#39; object has no attribute &#39;SSL_ST_INIT&#39;(转)
- 4.html基础标签:表单
- SpringMVC系列(一)SpringMVC概述和搭建SpringMVC的第一个helloWord入门程序
- 39. 在linux下装好Tomcat要给 tomcat/bin/下面所有.sh的文件执行权限
- 在java工程中导入jar包的注意事项
- python抓包模块
- 辗转相除法 &; 裴蜀定理
热门文章
- 将ESXI所有的端口组迁移到分布式交换机的步骤
- [UE4]蓝图Get Control Rotation获取人物角色朝向,设置默认人物相机,朝向与controller绑定
- [UE4] C++实现Delegate Event实例(例子、example、sample)
- javascript的密封对象之seal(),isSealed()方法
- TensorFlow相关的一些技巧
- GD库简介和使用
- uva-784-水题-搜索
- B站上的一个MATLAB与神经网络的视频,捡漏
- clip-path的任意元素的碎片拼接动效
- requests bs4 爬取 资讯 图片