题目描述:

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

解题思路:

遍历两次数组,第一次从前往后遍历,每个位置存放的是之前所有数字的乘积,第二次从后往前遍历,可以得到该位置之后所有数字的乘积,这样就得到每个位置除了本身之外的所有位置乘积。

代码如下:

public class Solution {
public int[] productExceptSelf(int[] nums) {
int n = nums.length;
int[] res = new int[n];
for(int i = 0, temp = 1; i < n; i++){
res[i] = temp;
temp *= nums[i];
}
for(int i = n - 1, temp = 1; i >= 0; i--){
res[i] *= temp;
temp *= nums[i];
}
return res;
}
}

  

Java [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 除本身之外的数组之积

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

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

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

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

  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. VIM Taglist安装配置和使用

    问题描述:            VIM  Taglist安装于配置 问题解决:             (1)安装Taglist包      (2)解压taglist压缩包         (3)将 ...

  2. [转载]C#获取本机IPv4地址

    C#获取本机IP地址在C#1.0之后都使用下面的这种形式: IPHostEntry ipe = Dns.GetHostEntry(Dns.GetHostName()); IPAddress ipa=i ...

  3. [转载]c# OpenFileDialog

    string resultFile = ""; OpenFileDialog openFileDialog1 = new OpenFileDialog();            ...

  4. 当IIS挂的网站出现选 图片文件, 静态文件都打不开时, 可以试试新建一个应用程序池试试看...

    当IIS挂的网站出现选 图片文件, 静态文件都打不开时, 可以试试新建一个应用程序池试试看...

  5. [设计模式] 23 访问者模式 visitor Pattern

    在GOF的<设计模式:可复用面向对象软件的基础>一书中对访问者模式是这样说的:表示一个作用于某对象结构中的各元素的操作.它使你可以在不改变各元素的类的前提下定义作用于这些元素的新操作.访问 ...

  6. 关于StringBuilder

    写在前面的话 很久没有更新博客了,来上海实习身边的一切波动挺大的,还好我走过来了,博客园:一路有你! StringBuilder 相信大家对StringBuilder类型一定不陌生,我们Coding经 ...

  7. 将LINUX变成路由器

    将LINUX变成路由器 2009-06-04 22:38:45 标签:LINUX 路由器 休闲 职场 版权声明:原创作品,如需转载,请与作者联系.否则将追究法律责任.     LINUX系统是一个强大 ...

  8. 【hadoop2.6.0】MapReduce原理

    看了几篇博文,感觉还是云里雾里的. http://blog.csdn.net/opennaive/article/details/7514146 http://www.aboutyun.com/thr ...

  9. [C++]虚函数-同名访问

    首先来看一下派生类和基类成员同名事的处理规则: 派生类内定义了一个与基类同名的成员,该现象称为同名覆盖,此时,无论派生类内部成员函数还是派生类的对象访问同名成员,如果未加任何特殊标识,则访问派生类中重 ...

  10. hdu1116

    http://acm.hdu.edu.cn/showproblem.php?pid=1116 #include<stdio.h> #include<math.h> #inclu ...