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 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的更多相关文章
- [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 ...
- 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 ...
- 【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 ...
- 【08_238】Product of Array Except Self
Product of Array Except Self Total Accepted: 26470 Total Submissions: 66930 Difficulty: Medium Given ...
- 【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 ...
- LeetCode OJ 238. Product of Array Except Self 解题报告
题目链接:https://leetcode.com/problems/product-of-array-except-self/ 238. Product of Array Except Se ...
- 238. Product of Array Except Self(对O(n)和递归又有了新的理解)
238. Product of Array Except Self Total Accepted: 41565 Total Submissions: 97898 Difficulty: Med ...
- LintCode 373: Partition Array
LintCode 373: Partition Array 题目描述 分割一个整数数组,使得奇数在前偶数在后. 样例 给定[1, 2, 3, 4],返回[1, 3, 2, 4]. Thu Feb 23 ...
- leetcode:238. Product of Array Except Self(Java)解答
转载请注明出处:z_zhaojun的博客 原文地址 题目地址 Product of Array Except Self Given an array of n integers where n > ...
随机推荐
- 【php学习】图片操作
前两天要对一张图片进行处理,其实很简单,就是在图片上加上字符串,一个图片而已,但是自己如同得了短暂性失忆似的,图片操作的函数一个都想不起来.所以就抽空整理了一下图片操作函数. 图片处理三步走: 创建画 ...
- TM2013修改帐号数据目录
M 2013安装以后,聊天记录文件夹默认的保存位置是在“我的文档”中“Tencent Files”,而QQ就可以在软件系统设置中进行指定,但TM2013没有这一栏设置,那么如何才能修改聊天记录文件夹保 ...
- 【转】用树莓派搭建web服务器
本文将详细介绍如何在树莓派上配置服务器,和<教你在Xubuntu上搭建LAMP服务器>有些类似,多了一些介绍在树莓派上的不同步骤的地方. 这种服务器的配置被称为LAMP,是最流行的服务器配 ...
- 【C51】单片机定时器介绍
标准51架构的单片机有2个定时器 :T0 和 T1,他们2个的用法几乎一样.下面主要讲T0定时器的用法. 初步认知 定时器 和 计数器 都是单片机中同一个模块.他们的实质都是: 加法存储计数器.对 ...
- 在VC6.0中编译头文件时产生moc文件
1.在FileView视图中 右键点击需要产生moc文件的头文件(就是类中包含Q_OBJECT宏,如果没有这个宏就不需要产生moc文件) 2.在右键菜单中选择Setting... 3.选择Custom ...
- [ASP.NET] Dictionary 和 Hashtable 区别
Dictionary和Hashtable 是两个比较常用的表示键/值的集合,两者在实际使用过程中有何区别呢? 具体区别如下: 1. Hashtable不支持泛型,而Dictionary支持泛型. 2. ...
- CentOS7+Redis Live安装配置
Redis Live是一个用来监控redis实例,分析查询语句并且有web界面的监控工具,使用python编写. 代码下载地址:https://github.com/nkrode/RedisLive ...
- Java调用ASP.NET的webservice故障排除
公司要接入其它公司的一个业务功能,对方是提供的 .net产生的webservice,在用cxf的wsdl2java命令生成客户端的测试代码时,出现了如下故障WSDLToJava Error: Thro ...
- [LeetCode]题解(python):112 Path Sum
题目来源 https://leetcode.com/problems/path-sum/ Given a binary tree and a sum, determine if the tree ha ...
- JS之call/apply/bind
测试代码: var a = 1; var obj = { a = 2; } function test(a){ alert(a); alert(this.a); } 1.test(3); 结果:3,1 ...