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], B is [6, 3, 2]

非常典型的Forward-Backward Traversal 方法:

但是第一次做的时候还是忽略了一些问题:比如A.size()==1时,答案应该是空[]

 public class Solution {
/**
* @param A: Given an integers array A
* @return: A Long array B and B[i]= A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1]
*/
public ArrayList<Long> productExcludeItself(ArrayList<Integer> A) {
// write your code
ArrayList<Long> res = new ArrayList<Long>();
if (A==null || A.size()==0 || A.size()==1) return res;
long[] lProduct = new long[A.size()];
long[] rProduct = new long[A.size()];
lProduct[0] = 1;
for (int i=1; i<A.size(); i++) {
lProduct[i] = lProduct[i-1]*A.get(i-1);
}
rProduct[A.size()-1] = 1;
for (int j=A.size()-2; j>=0; j--) {
rProduct[j] = rProduct[j+1]*A.get(j+1);
}
for (int k=0; k<A.size(); k++) {
res.add(lProduct[k] * rProduct[k]);
}
return res;
}
}

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

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

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

  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. LintCode 373: Partition Array

    LintCode 373: Partition Array 题目描述 分割一个整数数组,使得奇数在前偶数在后. 样例 给定[1, 2, 3, 4],返回[1, 3, 2, 4]. Thu Feb 23 ...

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

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

随机推荐

  1. Greedy_algorithm

    https://en.wikipedia.org/wiki/Greedy_algorithm

  2. InnoDB , MyISAM :MySQL 5.7 Supported Storage Engines

    http://dev.mysql.com/doc/refman/5.7/en/storage-engines.html https://en.wikipedia.org/wiki/ACID https ...

  3. 关于使用注解出现BeanCreationException或者NameNotFoundException的解决方法

    网上大部分解决方法是修改配置文件,但是本人修改后发现还是报错,只能耐着头皮继续看下去,最后发现是path出错,注意web.xml中的<resource-ref>的<res-ref-n ...

  4. php YAF

    Yaf 的特点: 用C语言开发的PHP框架, 相比原生的PHP, 几乎不会带来额外的性能开销. 所有的框架类, 不需要编译, 在PHP启动的时候加载, 并常驻内存. 更短的内存周转周期, 提高内存利用 ...

  5. yaf性能测试(wamp环境)

    1实现mvc 出现helloword,成功 2.controller重定向 $get = $this->getRequest()->getQuery("get", &q ...

  6. Linux上free命令的输出

    一.明确概念 A buffer is something that has yet to be "written" to disk.  A cache is something t ...

  7. .NET对象与Windows句柄(二):句柄分类和.NET句柄泄露的例子

    上一篇文章介绍了句柄的基本概念,也描述了C#中创建文件句柄的过程.我们已经知道句柄代表Windows内部对象,文件对象就是其中一种,但显然系统中还有更多其它类型的对象.本文将简单介绍Windows对象 ...

  8. BI 商业智能理解结构图

    本文章是在本人实习阶段对BI(商业智能 Business Intelligence)的理解:(如有不足之处请多指教,谢谢) BI 系统负责从多个数据源中搜集数据,并将这些数据进行必要的转换后存储到 ...

  9. OO之美3

    面向对象和基于对象 基于对象:所以基于对象,就是一种对数据类型的抽象,封装一个结构包含了数据和函数,然后以对象为目标进行操作.构建的基础是对象,但是操作对象并不体现出面向对象的继承性,也就是基于对象局 ...

  10. 控制台打印出event对象时,对象里面的currentTarget为null

    但是MouseEvent对象展开时 附上老外的解释~~~: http://stackoverflow.com/questions/26496176/when-loggin-an-event-objec ...