Given an integers array A.

Define B[i] = A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1], calculate B WITHOUT divide operation.

Example

For A = [1, 2, 3], return [6, 3, 2].

分析:

 public class Solution {
public int[] productExceptSelf(int[] A) {
int[] left = new int[A.length];
int[] right = new int[A.length];
int[] result = new int[A.length]; for (int i = ; i < A.length; i++) {
left[i] = i == ? : left[i - ] * A[i - ];
right[A.length - - i] = (i == ) ? : right[A.length - i] * A[A.length - i];
} for (int i = ; i < A.length; i++) {
result[i] = left[i] * right[i];
}
return result;
}
}
 public class Solution {
public int[] productExceptSelf(int[] nums) {
int n = nums.length;
int[] res = new int[n];
res[] = ;
for (int i = ; i < n; i++) {
res[i] = res[i - ] * nums[i - ];
}
int right = ;
for (int i = n - ; i >= ; i--) {
res[i] *= right;
right *= nums[i];
}
return res;
}
}

one pass

 class Solution {
public int[] productExceptSelf(int[] nums) {
int[] result = new int[nums.length];
Arrays.fill(result, );
int left = , right = ;
for (int i = , j = nums.length - ; i < nums.length - ; i++, j--) {
left *= nums[i];
right *= nums[j];
result[i + ] *= left;
result[j - ] *= right;
}
return result;
}
}

Product of Array Exclude Itself的更多相关文章

  1. Lintcode: Product of Array Exclude Itself

    Given an integers array A. Define B[i] = A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1], calculate B wi ...

  2. [LintCode] Product of Array Except Self 除本身之外的数组之积

    Given an integers array A. Define B[i] = A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1], calculate B WI ...

  3. 【LeetCode】Product of Array Except Self

    Product of Array Except Self Given an array of n integers where n > 1, nums, return an array outp ...

  4. 【08_238】Product of Array Except Self

    Product of Array Except Self Total Accepted: 26470 Total Submissions: 66930 Difficulty: Medium Given ...

  5. 【LeetCode】238. Product of Array Except Self

    Product of Array Except Self Given an array of n integers where n > 1, nums, return an array outp ...

  6. LeetCode OJ 238. Product of Array Except Self 解题报告

        题目链接:https://leetcode.com/problems/product-of-array-except-self/ 238. Product of Array Except Se ...

  7. 238. Product of Array Except Self(对O(n)和递归又有了新的理解)

    238. Product of Array Except Self     Total Accepted: 41565 Total Submissions: 97898 Difficulty: Med ...

  8. leetcode:238. Product of Array Except Self(Java)解答

    转载请注明出处:z_zhaojun的博客 原文地址 题目地址 Product of Array Except Self Given an array of n integers where n > ...

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

随机推荐

  1. Mysql-字段类型

    首先统计所有,以表格查看 数字类型 列类型 需要的存储量 TINYINT 1 字节 SMALLINT 2 个字节 MEDIUMINT 3 个字节 INT 4 个字节 INTEGER 4 个字节 BIG ...

  2. Jquery-获取父级元素parent

    1. parent([expr]): 获取指定元素的所有父级元素 <div id="par_div"><a id="href_fir" hre ...

  3. 状态压缩dp问题

    问题:Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Ev ...

  4. POJ2286 The Rotation Game

    Description The rotation game uses a # shaped board, which can hold 24 pieces of square blocks (see ...

  5. 从ICLassFactory 为 CLSID的COM组建创建实例失败:c001f011

    在sqlserver创建计划任务的时候,保存时出现:“从ICLassFactory 为 CLSID的COM组建创建实例失败:c001f011”. 解决方法:在运行sqlserver时,使用“以管理员身 ...

  6. MAC OS下安装Erlang

    这是个很大的问题,也是个很小的问题,恩.因为我最终解决方案是用了别人写的工具,名叫erlbrew: https://github.com/mrallen1/erlbrew 按照页面上的指导安装使用即可 ...

  7. POJ2586Y2K Accounting Bug(贪心 + 不好想)

    Y2K Accounting Bug Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12251   Accepted: 62 ...

  8. MyEclipse------File类的各种方法

    usingFile.jsp <%@ page language="java" import="java.util.*" pageEncoding=&quo ...

  9. 15个Linux Wget下载实例终极指南

    15个Linux Wget下载实例终极指南 Linux wget是一个下载文件的工具,它用在命令行下.对于Linux用户是必不可少的工具,尤其对于网络管理员,经常要下载一些软件或从远程服务器恢复备份到 ...

  10. Google Protocol Buffer 的使用和原理

    Google Protocol Buffer 的使用和原理 Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,很适合做数据存储或 RPC 数据交换格式.它 ...