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. android数据存储之File

    android中使用File进行存储主要使用到OpenFileOutput和OpenFileInput两个方法,下面直接用一个例子来说明一下. (1)布局文件main.xml文件 <?xml v ...

  2. C#执行DOS命令(CMD命令)

    在c#程序中,有时会用到调用cmd命令完成一些功能,于是在网上查到了如下方法,实现了c#执行DOS命令,并返回结果.         //dosCommand Dos命令语句         publ ...

  3. C#Winform中treeView控件使用总结

    1.如何展开结点时改变图标(注意:不是选中时) 要在目录中使用图标首先要加入一个控件ImageList(命名为imageList1),然后可以按图片的index或名称引用图片. 然后需要在TreeVi ...

  4. exp命令ORACLCE10G导出ORACLE11G的数据1455错误

    异常:1455 解决: 命令:exp youruser/password@192.xxx.x.xx:1521/orcl owner=youruser file=c:\export.dmp trigge ...

  5. Android 使用AIDL调用外部服务

    好处:多个应用程序之间建立共同的服务机制,通过AIDL在不同应用程序之间达到数据的共享和数据相互操作, 本文包括: 1 .创建AIDL 服务端.2 .创建AIDL 客户端. 3.客户端调用服务端提供的 ...

  6. sql 操作,

    SELECT * FROM dbo.tbl_Web_Photos AS pt LEFT JOIN dbo.tbl_Web_Friends AS fd ON pt.user_email=fd.AddEm ...

  7. LintCode "The Smallest Difference"

    Binary search. class Solution { int _findClosest(vector<int> &A, int v) { , e = A.size() - ...

  8. activiti自定义流程之自定义表单(一):环境配置

    先补充说一下自定义流程整个的思路,自定义流程的目的就是为了让一套代码解决多种业务流程,比如请假单.报销单.采购单.协作单等等,用户自己来设计流程图. 这里要涉及到这样几个基本问题,一是不同的业务需求, ...

  9. 黄聪:MYSQL提交一批ID,查询数据库中不存在的ID并返回

    假设你数据库有个A表: ID NAME    1 aaa    2 bbb    3 ccc    4 ddd 需求:给你几个ID,返回A表中不存在的ID? 例如提交1,2,8,9 返回8,9 sel ...

  10. PLSQL_性能优化系列13_Oracle Index Rebuild索引重建

    2014-10-04 Created By BaoXinjian