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; }
}

最新文章

  1. 9.JAVA中的正则表达式
  2. urlscan使用详解
  3. 你一定能用的上的iOS第三方库
  4. 2048-AI程序算法分析
  5. 利用rlwrap配置linux下oracle sqlplus 历史记录回调
  6. 区分jquery中的offset和position
  7. Fedora 21 安装QQ国际版
  8. Hash表——The Hash table
  9. 9.XML文件解析
  10. JS——基础知识(三)
  11. codeforce vk cup2017
  12. Python安装与环境变量
  13. delphi 多线程之System.TMonitor
  14. python问题:AttributeError: &#39;module&#39; object has no attribute &#39;SSL_ST_INIT&#39;(转)
  15. 4.html基础标签:表单
  16. SpringMVC系列(一)SpringMVC概述和搭建SpringMVC的第一个helloWord入门程序
  17. 39. 在linux下装好Tomcat要给 tomcat/bin/下面所有.sh的文件执行权限
  18. 在java工程中导入jar包的注意事项
  19. python抓包模块
  20. 辗转相除法 &amp; 裴蜀定理

热门文章

  1. 将ESXI所有的端口组迁移到分布式交换机的步骤
  2. [UE4]蓝图Get Control Rotation获取人物角色朝向,设置默认人物相机,朝向与controller绑定
  3. [UE4] C++实现Delegate Event实例(例子、example、sample)
  4. javascript的密封对象之seal(),isSealed()方法
  5. TensorFlow相关的一些技巧
  6. GD库简介和使用
  7. uva-784-水题-搜索
  8. B站上的一个MATLAB与神经网络的视频,捡漏
  9. clip-path的任意元素的碎片拼接动效
  10. requests bs4 爬取 资讯 图片