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 > ...
随机推荐
- npm reset config
To reset user defaults Run this in the command line (or git bash on windows): echo "" > ...
- OpenGL完全教程 第一章 初始化OpenGL
第一章 初始化OpenGL 无论是什么东西,要使用它,就必须对它进行初始化.如果你之前使用过GDI,你应该也多多少少了解到GDI在绘制图形之前要为之创建渲染环境.OpenGL也一样.本章给出的代码,大 ...
- C 判断路径存在
1 用 int access(const char *pathname, int mode); 判断有没有此文件或目录 --它区别不出这是文件还是目录2 用 i ...
- 蓝牙4.0(包含BLE)简介
1. BLE (低功耗蓝牙)简介 国际蓝牙联盟( BT-SIG,TI 是 企业成员之一)通过的一个标准蓝牙无线协议. 主要的新特性是在蓝牙标准版本上添加了4.0 蓝牙规范 (2010 年6 月 ...
- mysql case when用法
SELECT CASE WHEN `categoryid` =1THEN '参赛队员'ELSE '指导老师'END FROM `blog_article` WHERE 1
- OpenSSL-for-IOS
http://download.csdn.net/download/kokjuis/9723109
- ArcGIS Portal 10.4 本地坐标系的web 3d地形展示制作说明
原文:ArcGIS Portal 10.4 本地坐标系的web 3d地形展示制作说明 ArcGIS Portal 10.4 本地坐标系的web 3d地形展示制作说明 By 李远祥 ArcGIS Por ...
- mysql 数据库授权
1: GRANT ALL PRIVILEGES ON share_db.* to 'db_user'@'%' IDENTIFIED BY 'db_user'; 2:FLUSH PRIVILEGES;
- Android SDK无法更新问题解决 ---- 还可解决无法上google的问题
Android SDK无法更新问题解决 2012-10-14 10:13:01| 分类: Android|举报|字号 订阅 1.在SDK Manager下Tools->Options打 ...
- 二级c程序设计题(1)
原文:http://www.cnblogs.com/imaker/p/6120951.html 所属年份:2010.9;2011.3;2012.3;请编写函数fun,其功能是:找出2×M整型二维数组中 ...