【LintCode】Product of Array Exclude Itself 数组剔除元素后的乘积
2015-07-15 12:04
267 查看
给定一个整数数组A。
定义B[i] = A[0] * … * A[i-1] * A[i+1] * … * A[n-1], 计算B的时候请不要使用除法。
样例
给出A=[1, 2, 3],返回 B为[6, 3, 2]
定义B[i] = A[0] * … * A[i-1] * A[i+1] * … * A[n-1], 计算B的时候请不要使用除法。
样例
给出A=[1, 2, 3],返回 B为[6, 3, 2]
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) { ArrayList<Long> B = new ArrayList<Long>(); Long mul = 1l; for(int i = 0; i < A.size(); i++) { for(int j = 0; j < A.size(); j++) { if(i != j){ mul *= A.get(j); } } B.add(mul); mul = 1l; } return B; } }
相关文章推荐
- PHP + Memcache 实现多服务器session共享
- EF中数据库切换为本地
- Android 启动页面弹出效果
- UVA108 Maximum Sum dp/暴力+简化
- 多线程基础知识第二篇:线程常用方法及各种状态
- 杭电 hdu 1546 Idiomatic Phrases Game (最短路径 + Dijkstra)
- 无意搜集到的一个mysql封装函数(php)
- iOS开发block的学习
- Windows 8.1 x64下 编译和使用Boost
- 【一天一个canvas】阴影效果呈现方法(十一)
- [leedcode 83] Remove Duplicates from Sorted List
- CSS3的REM设置字体大小
- codeforces 558A. Lala Land and Apple Trees
- linux下目录的作用
- DataHub
- unity3d calculatepath
- afnetworking设置请求头代码
- 彩色图像灰度化之一 问题提出与国内外研究梗概
- Creating a Physical Standby Database 11g
- POJ 1655 Balancing Act 焦点树