LeetCode[238] Product of Array Except Self
来源:互联网 发布:sublime java高亮显示 编辑:程序博客网 时间:2024/06/07 05:44
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 ofnums
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.)
class Solution {public:vector<int> productExceptSelf(vector<int>& nums) {int product = 1, zeroCount = 0;for (int i = 0; i < nums.size(); i++){if (nums[i] == 0){zeroCount++;continue;}product *= nums[i];}if (zeroCount == 0){vector<int> result;for (int i = 0; i < nums.size(); i++)result.push_back(product / nums[i]);return result;}else{vector<int> result(nums.size(), 0);if (zeroCount == 1){for (int i = 0; i < nums.size(); i++){if (nums[i] == 0)result[i] = product;}}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
- 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
- Kafka 学习笔记
- static块
- java内存模型
- 初学者从oracle到mysql--分页
- Learn from vimtutor
- LeetCode[238] Product of Array Except Self
- 文章标题
- hdu 5900 QSC and Master 区间dp
- [Leetcode] Contains Duplicate II
- 伪静态,防盗链,网址错误
- JAVA EE-SERVLET
- 群英传之Android 5.X过渡动画
- 养老评估--老年人能力评估信息平台和数据库--9.18
- 测量目标对象行走的速度---WiFi Signals