[LeetCode]238. Product of Array Except Self
来源:互联网 发布:离线基站数据库 300万 编辑:程序博客网 时间:2024/06/05 01:56
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]
.
思路:每个结果由这个元素左边所有元素乘积*右边所有元素乘积,所以先用数组装下每个元素左边的乘积,然后倒序,用一个变量记住右边乘积,再相乘即为结果
public class Solution { public int[] productExceptSelf(int[] nums) { int[] res=new int[nums.length]; res[0]=1; for(int i=1;i<nums.length;i++){ res[i]=res[i-1]*nums[i-1]; } int right=1; for(int i=nums.length-2;i>=0;i--){ right=right*nums[i+1]; res[i]=res[i]*right; } 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
- 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
- JavaWeb开发之JSP隐式对象(二)
- Swift服务器入门+ReactJS代码示范:如何在内存中建立高性能索引
- 杭电ACM 1004 Let the Balloon Rise
- 离散对数和原根
- IMWeb训练营作业——使用vue完成todolist
- [LeetCode]238. Product of Array Except Self
- 集合框架-Collection集合-3
- CASIO DC-850 DATA-CAL Manual (操作使用方法)
- 9个offer,12家公司,35场面试,从微软到谷歌,应届计算机毕业生的2012求职之路
- 物联网时代来了,大数据也终于有了用武之地
- uva 572
- 字符串算法常用的操作
- 科锐课堂笔记:2017/4/19 虚函数
- 基于网络请求框架的封装