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. python 切片

    本人的博客中的python内容基本上全是看着廖雪峰大神博客做的一个笔记 关于列表或者元祖的切片 下面说一下列表的切片的语法: L[起始位置:结束位置:步长] number = range(100) n ...

  2. HTML5动画实例

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  3. poj2406 KMP

    kmp简单题 找循环节.由于KMP的next[]数组,所以可以考虑最后一组的情况,及next[n]的值:n-next[n]的值表示一个循环节. 如果n%(n-next[n])!=0表明该循环不成立.不 ...

  4. FastDFS在.Net平台上的使用

    上一篇,了解了FastDFS是什么东东,一般稍微大一的网站都会做文件分离存储,FastDFS这轻型的分布式文件存储方式,非常有用. 此图片截取博友(张占岭)的勿喷 下面我们就了解一下,FastDFS在 ...

  5. python多态

    多态是面向对象语言的一个基本特性,多态意味着变量并不知道引用的对象是什么,根据引用对象的不同表现不同的行为方式.在处理多态对象时,只需要关注它的接口即可,python中并不需要显示的编写(像Java一 ...

  6. Java获取各种常用时间方法大全

    Java获取各种常用时间方法大全 package cc.javaweb.test; Java中文网,Java获取各种时间大全 import java.text.DateFormat; import j ...

  7. Java编程思想学习(六) 多态

    1.Java语言的三大特性:继承.封装和多态. 继承:复用类的一种方法,可以简省很多代码: 封装:通过合并特征和行为来创建新的数据类型.[这种“数据类型”跟Java本身提供的8大“基本数据类型”的地位 ...

  8. myeclipse 部署应用

    昨天把MyEclipse10给安装上了,今天想在MyEclipse下启动Tomcat并在浏览器中看到写的Web页面,但是当在浏览器中输入地址时,出现了404错误,出现这个错误的原因是因为没有找到指定的 ...

  9. 改变了Tomcat路径后无法卸载和重装的解决办法

    错误如下: ---------------------------Apache Tomcat Setup---------------------------Failed to install Tom ...

  10. Unity-Tween

    1.GoKit 免费开源 AssetStore:https://www.assetstore.unity3d.com/en/#!/content/3663 下载地址:https://github.co ...