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

[leetcode]238. Product of Array Except Self除了自身以外的数组元素乘积的更多相关文章

  1. [LeetCode] 238. Product of Array Except Self 除本身之外的数组之积

    Given an array nums of n integers where n > 1,  return an array output such that output[i] is equ ...

  2. LeetCode 238. Product of Array Except Self (去除自己的数组之积)

    Given an array of n integers where n > 1, nums, return an array output such that output[i] is equ ...

  3. LN : leetcode 238 Product of Array Except Self

    lc 238 Product of Array Except Self 238 Product of Array Except Self Given an array of n integers wh ...

  4. 剑指offer 66. 构建乘积数组(Leetcode 238. Product of Array Except Self)

    剑指offer 66. 构建乘积数组 题目: 给定一个数组A[0, 1, ..., n-1],请构建一个数组B[0, 1, ..., n-1],其中B中的元素B[i] = A[0] * A[1] * ...

  5. (medium)LeetCode 238.Product of Array Except Self

    Given an array of n integers where n > 1, nums, return an array output such that output[i] is equ ...

  6. Java [Leetcode 238]Product of Array Except Self

    题目描述: Given an array of n integers where n > 1, nums, return an array output such that output[i]  ...

  7. C#解leetcode 238. Product of Array Except Self

    Given an array of n integers where n > 1, nums, return an array output such that output[i] is equ ...

  8. leetcode 238 Product of Array Except Self

    这题看似简单,不过两个要求很有意思: 1.不准用除法:最开始我想到的做法是全部乘起来,一项项除,可是中间要是有个0,这做法死得很惨. 2.空间复杂度O(1):题目说明了返回的那个数组不算进复杂度分析里 ...

  9. Leetcode 238 Product of Array Except Self 时间O(n)和空间O(1)解法

    1. 问题描写叙述 给定一个n个整数的数组(n>1n>1)nums,返回一个数组output,当中的元素outputioutput_i的值为原数组nums中除numsinums_i之外的全 ...

随机推荐

  1. 在控制终端输入AT命令

    控制台终端输入AT命令到smd8,步骤如下: 1. 先执行命令 cat /dev/smd8 & 2. 再执行  echo  -e  "ati\r\n"  > /dev ...

  2. C++ 无锁队列实现

    上源码 #ifndef __GLOBAL_LOCK_FREE_QUEUE_H__ #define __GLOBAL_LOCK_FREE_QUEUE_H__ #include <atomic> ...

  3. 开发组件:REST API

    REST API 最佳入门指南 https://blog.csdn.net/px01ih8/article/details/78674685

  4. MySQL数据库索引(中)

    上一篇回顾: 1.一个索引对应一颗B+树,所有的真实记录都是存在叶子节点里面的,所有的项目录都存在内节点或者说根节点上. 2.innodb会为我们的表格主键添加一个聚簇索引,如果没有主键的话数据库是会 ...

  5. jshint在bat批处理中闪退,代码中无法调用的问题

    先说解决办法:加个call eg: call jshint --version Pause 具体原因有空再更

  6. https://127.0.0.1:8080/test?param={%22..报错

    使用场景:spring boot 1.5.x,内置的tomcat版本为8.5.1 原因: tomcat自tomcat 8.0.35版本之后对URL参数做了比较规范的限制,必须遵循RFC 7230 an ...

  7. BP神经网络的公式推导

    如果感觉自己看不懂,那就看看我博客的梯度下降法,博文最后的感知机也算最简单的BP神经网络吧,用的也是反馈(w,b):典型梯度下降法 BP网络的结构 BP网络的结构如下图所示,分为输入层(Input), ...

  8. LINUX漏洞-安全防护--防火墙相关

    漏洞扫描 https://blog.csdn.net/e_Inch_Photo/article/details/79072360 基本安全防范: https://blog.csdn.net/holmo ...

  9. centos7配置vsftpd服务器

    参考网站:https://blog.csdn.net/lianghongge/article/details/78209445 ==================================== ...

  10. CSS 3栏自适应布局

    绝对定位 css html,body{margin: 0px;height:100%;} div{height: 100%;} .left,.right {top: 0px;position: abs ...