Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].

Solve it without division and in O(n).

For example, given [1,2,3,4], return [24,12,8,6].

Follow up:
Could you solve it with constant space complexity? (Note: The output array does not count as extra space for the purpose of space complexity analysis.)

解法:output[p]=pre[p]*after[p]

代码如下:

public class Solution {
public int[] productExceptSelf(int[] nums) {
int len=nums.length;
int[] output=new int[len];
int[] pre=new int[len];
int[]after=new int[len];
for(int i=0;i<len;i++)
pre[i]=1;
for(int j=0;j<len;j++)
after[j]=1;
for(int m=1;m<len;m++){
pre[m]=pre[m-1]*nums[m-1];
}
for(int n=len-2;n>=0;n--){
after[n]=after[n+1]*nums[n+1];
}
for(int p=0;p<len;p++){
output[p]=pre[p]*after[p];
}
return output;
}
}

  运行结果:时间复杂度O(n)

(medium)LeetCode 238.Product of Array Except Self的更多相关文章

  1. 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 ...

  2. 剑指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] * ...

  3. [leetcode] 238. Product of Array Except Self (medium)

    原题 思路: 注意时间复杂度,分别乘积左右两边,可达到O(n) class Solution { public: vector<int> productExceptSelf(vector& ...

  4. [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 ...

  5. 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除了自身以外的数组元素乘积

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

  9. leetcode 238 Product of Array Except Self

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

随机推荐

  1. AMD规范:define和require的区别

    define和require在依赖处理和回调执行上都是一样的,不一样的地方是define的回调函数需要有return语句返回模块对象(注意是对象),这样define定义的模块才能被其他模块引用:req ...

  2. 解决504 Gateway Time-out(nginx)

    504 Gateway Time-out问题常见于使用nginx作为web server的服务器的网站 我遇到这个问题是在需要插入一万多条数据时候遇到的 一般看来, 这种情况可能是由于nginx默认的 ...

  3. 利用百度地图API,获取经纬度坐标

    利用百度地图API,获取经纬度坐标 代码很简单,但在网上没找到现成的获取地图经纬度的页面. 就是想,给当前页面传递一个经纬度,自动定位到此经纬度.然后可以重新选择,选择完返回经纬度. 效果如下: 源代 ...

  4. php 添加redis扩展(一)

    phpredis 下载地址 http://pan.baidu.com/s/1dFFK0gh  提取码:见评论,(~..)亲身试验,可行 1.下载到目录 cd /usr/local/src 2.解压 t ...

  5. LintCode "Subarray Sum II"

    Sliding window doesn't work. So it is a typical partial_sum base solution. As below. However if you ...

  6. 【Struts2学习笔记-3】常量配置

    Struts2常量 配置Struts2常量值有3个地方,1)在struts.properties文件中配置常量:2)在web.xml文件中配置FileterDispatcher指定初始化参数来配置常量 ...

  7. Puppet's Commands 3.7

    Puppet's Commands Puppet’s command line interface consists of a single puppet command with many subc ...

  8. elixir学习

    安装 brew install elixir atom配置 language-elixir atom-elixir elixir的shell iex :erlang.system_info(:otp_ ...

  9. linux 鼠标中键粘帖功能?!!

    转载自:http://yjhexy.iteye.com/blog/785564 ubuntu鼠标中键问题,其实也不是什么问题,ubuntu的鼠标中键是用来快速粘贴的,只是windows用惯了,时不时手 ...

  10. Apriori 关联算法学习

    1. 挖掘关联规则 1.1   什么是关联规则 一言蔽之,关联规则是形如X→Y的蕴涵式,表示通过X可以推导“得到”Y,其中X和Y分别称为关联规则的先导(antecedent或left-hand-sid ...