LeetCode 238. Product of Array Except Self
2016-07-22 02:03
337 查看
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].
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.)
Subscribe to see which companies asked this question
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.)
Subscribe to see which companies asked this question
class Solution { public: vector<int> productExceptSelf(vector<int>& nums) { vector<int> v(nums.size()); int right = 1; v[0] = 1; //左边所有数字的乘积 for(int i = 1; i < nums.size(); i++) { v[i] = nums[i - 1] * v[i - 1]; } for(int i = nums.size() - 2; i >= 0; i--) { right = right * nums[i + 1]; v[i] = v[i] * right; } return v; } };
相关文章推荐
- Matlab之文件读写
- 算法一(前言-动态规划)
- 星期问题
- LeetCode 204. Count Primes
- LeetCode 119. Pascal’s Triangle II
- LeetCode 189. Rotate Array
- 类的加载顺序
- Codeforces Beta Round #72 (Div. 2 Only)D. Doctor
- 固本培元之四:这个需要回头体会
- WebUploader的java后端上传代码(支持分片上传)(未测试)
- LeetCode 62. Unique Paths
- Codeforces Beta Round #72 (Div. 2 Only)D. Doctor
- HDU5737 : Differencia
- ftp 实现文件的上传下载以及列出文件列表Java代码(未测试)
- LeetCode 101. Symmetric Tree
- LeetCode 9. Palindrome Number
- LeetCode 107. Binary Tree Level Order Traversal II
- LeetCode 102. Binary Tree Level Order Traversal
- PHP 连接 MSSQL
- 套路重写Java的equals函数