238. Product of Array Except Self
来源:互联网 发布:java获取当前文件路径 编辑:程序博客网 时间:2024/04/27 23:16
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]
.
solution:
class Solution {public: vector<int> productExceptSelf(vector<int>& nums) { vector<int> res; int c = 1; for(int i=0; i<nums.size(); i++){ res.push_back(c); c *= nums[i]; } c = 1; for(int i=nums.size()-1; i>=0; i--){ res[i] *= c; c *= nums[i]; } 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
- 238. Product of Array Except Self
- 238. Product of Array Except Self
- 238. Product of Array Except Self
- 238. Product of Array Except Self
- [LeetCode]238. Product of Array Except Self
- 238. Product of Array Except Self
- 238. Product of Array Except Self
- 238. Product of Array Except Self
- 238. Product of Array Except Self
- LeetCode *** 238. Product of Array Except Self
- LeetCode 238. Product of Array Except Self
- 51单片机系列知识8--定时器、计数器(1)
- Wijmo5 Flexgrid基础教程:增加和删除
- 实现OC与JS的交互
- SQLSERVER存储过程基本语法
- Restful形式接口文档生成之Swagger与SpringMVC整合手记
- 238. Product of Array Except Self
- Exception in thread "Timer-2" java.lang.OutOfMemoryError: Java heap space
- 22. ggv
- zookeeper 场景:配置文件同步
- 23. gicqd
- c语言入门之项目3.7——用for循环输出一个等腰直角三角形
- Example #1 Serializing a single value with WDDX
- 给图片绘制水印需要用到位数上下文
- python爬虫之糗百段子