238. Product of Array Except Self
2016-04-17 07:22
429 查看
Given an array of n integers where n > 1,
Solve it without division and in O(n).
For example, given
Follow up:
Could you solve it with constant space complexity? (Note: The output array does not count as extra space for the purpose of space complexity analysis.)
Similar: 152. Maximum Product Subarray
42. Trapping Rain Water
nums, return an array
outputsuch that
output[i]is equal to the product of all the elements of
numsexcept
nums[i].
Solve it without division and in O(n).
For example, given
[1,2,3,4], return
[24,12,8,6].
Follow up:
Could you solve it with constant space complexity? (Note: The output array does not count as extra space for the purpose of space complexity analysis.)
Similar: 152. Maximum Product Subarray
42. Trapping Rain Water
public class Solution { public int[] productExceptSelf(int[] nums) { int[] output = new int[nums.length]; output[0] = 1; for (int i = 1; i < nums.length; i++) { output[i] = output[i-1] * nums[i-1]; } int right = 1; for (int i = nums.length-1; i>=0; i--) { output[i] *= right; right *= nums[i]; } return output; } }
相关文章推荐
- C++学习笔记
- Python正则表达式指南
- 几种并行模型比较
- LeetCode 83. Remove Duplicates from Sorted List
- 台灣VR內容產業聯盟_技術推廣中心
- 爬虫的道魔策略
- 调试技巧--Windows端口号是否被占用
- iOS aotoresizing的基本使用
- AD(Alitium Designer)中ROOM的使用
- iOS autolayout 的基本使用(用代码添加约束)
- LeetCode 202. Happy Number
- AltiumDesigner学习笔记
- 152. Maximum Product Subarray
- Android学习(25) -- 单元测试(JUnit Test)
- 循环冗余码校验
- 系统的可靠性计算
- LeetCode 300. Longest Increasing Subsequence(最长递增子序列)
- Castle中AdditionalInterfaces用法介绍
- 汉字区位码、国标码(交换码)和机内码转换方法
- Python爬虫利器requests详解