leetcode 238:Product of Array Except Self
来源:互联网 发布:网络大电影发行协议 编辑:程序博客网 时间:2024/05/16 04:55
Given an array of n integers where n > 1, nums
, return an array output
such that output[i]
is equal to the product of all the elements of nums
except nums[i]
.
Solve it without division and in O(n).
For example, given [1,2,3,4]
, return [24,12,8,6]
.
分析题目:既然不让用除法,就只能用乘法,用i左边的乘以i右边的就是结果啦。
代码如下:
public int[] productExceptSelf(int[] nums) { int len=nums.length; int[] res=new int[len]; res[len-1]=1; // 得到i右边所有数的乘积 for(int i=len-2;i>=0;i--) { res[i]=res[i+1]*nums[i+1]; } int left=1; //i右边的乘以i左边的即为结果 for(int i=0;i<=len-1;i++) { res[i]*=left; left*=nums[i]; } return res; }
0 0
- leetcode 238: Product of Array Except Self
- LeetCode 238: Product of Array Except Self
- Leetcode #238 Product of Array Except Self
- Leetcode 238 Product of Array Except Self
- [leetcode 238]Product of Array Except Self
- LeetCode(238)Product of Array Except Self
- leetcode 238: Product of Array Except Self
- Leetcode #238 Product of Array Except Self
- [LeetCode 238] Product of Array Except Self
- leetcode 238:Product of Array Except Self
- Product of Array Except Self - LeetCode 238
- LeetCode 238 Product of Array Except Self
- 【LEETCODE】238-Product of Array Except Self
- LeetCode 238:Product of Array Except Self
- leetcode 238 Product of Array Except Self
- leetcode 238: Product of Array Except Self
- [LeetCode 238] Product of Array Except Self
- LeetCode[238]Product of Array Except Self
- Android保存图片到系统图库
- string ut8编码 需要对"/"转码
- Activiti工作流(1):activiti简介
- python2.7+django1.8+mysql的模版显示数据的例子
- Spring.NET 基本使用
- leetcode 238:Product of Array Except Self
- GCD同步问题
- POJ——1979Red and Black
- session与cookie整理
- 无法定位 xxxx 与动态链接库 avcodec-xx.dll上
- Debug模式和Release模式区别
- memcache简单使用
- python pip国内镜像
- 二维码