Product of Array Exclude Itself
来源:互联网 发布:java自学能学会吗 编辑:程序博客网 时间:2024/05/17 09:45
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 { /** * @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 int n=A.size();ArrayList<Long> l=new ArrayList<Long>();if(n==0)return null;if(n==1&&A.get(0)==0){l.add((long)1);return l;}for(int i=0;i<n;i++)l.add((long) 1);//上三角for(int i=n-2;i>=0;i--){l.set(i,(long)(A.get(i+1))*l.get(i+1));}//下三角long tmp=(long) 1;for(int i=1;i<n;i++){ tmp=tmp*(long)(A.get(i-1));l.set(i,tmp*l.get(i));}return l;} }
0 0
- Product of Array Exclude Itself
- Product of Array Exclude Itself
- Product of Array Exclude Itself
- Lintcode - Product of Array Exclude Itself
- lintcode:Product of Array Exclude Itself
- [Lintcode]Product of Array Exclude Itself
- #50 Product of Array Exclude Itself
- Lintcode50 Product of Array Exclude Itself solution 题解
- 【LintCode】Product of Array Exclude Itself 数组剔除元素后的乘积
- 50.Product of Array Exclude Itself-数组剔除元素后的乘积(容易题)
- 238.[LeetCode]Product of Array Except itself
- 238.[LeetCode]Product of Array Except itself
- 88-Product of Array
- get product of array
- Product of Array Except Self
- Product of Array Except Self
- Product of Array Except Self
- Product of Array Except Self
- 1979 第K个数 (真头疼)
- Codecademy-中文JavaScript系列教程-Function
- 第11周项目0是春哥啊
- 一起talk Vim吧(第十七回:Vim插件之cscope)
- 统计并输出英语短文中单词出现的次数
- Product of Array Exclude Itself
- Android-自定义组件之自定义条形频谱
- 数字信道化(digital channelizer)以及DFT(FFT)的深入理解
- MFC学习(15)COleDateTime类型的应用
- assert用法
- java基础知识小盘点2
- Network: Over-Wall
- vs2012快捷键
- 基于http协议的app缓存机制