LeetCode#238. Product of Array Except Self
来源:互联网 发布:云影源码1llo 编辑:程序博客网 时间:2024/06/05 18:10
- 题目:给定一个数组,计算除本身以外的所有元素乘积,第i个元素以外的元素乘积为输出结果的第i个元素
- 难度:Medium
- 思路:思路参考Discuss里,第i个输出结果为nums[i]左边的元素乘积乘以右边元素的乘积(以nums[i]将数组分成两部分)
- 代码:
public class Solution { public int[] productExceptSelf(int[] nums) { if(nums == null || nums.length == 0){ return null; } int len = nums.length; int[] result = new int[len];//result[i]表示nums[i]左边元素的乘积 result[0] = 1; for(int i = 1; i < len; i++){ result[i] = result[i-1]*nums[i-1]; } int right = 1; //用左边的元素乘以右边的元素,right是右边的元素 for(int i = len-1; i >= 0; i--){ result[i]*=right; right*=nums[i]; } return result; }}
阅读全文
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
- 238. Product of Array Except Self LeetCode
- 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
- 【leetcode】238. Product of Array Except Self
- leetcode:238. Product of Array Except Self
- 阿里云ubuntu16.4部署docker
- 使用中间件
- Maven中的-D(Properties属性)和-P(Profiles配置文件)
- 生成论文参考文献
- Android Studio运行程序出现Session ‘app’: Error Launching activity 解决办法
- LeetCode#238. Product of Array Except Self
- 面向对象程序设计概论
- iOS 数组/字典转json
- zookeeper+kafka
- GDI
- 集合类复习
- Lintcode——两数之和
- 【tarjan】17.6.1 仙人球 题解
- shell 数组 循环