leetcode 238. Product of Array Except Self
来源:互联网 发布:js时间差值 编辑:程序博客网 时间:2024/06/06 17:15
238. Product of Array Except Self
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.)
思路是:两个数组,left[] right[] 分别存i元素左右的乘积。但是left[]用ret[]来存,right[]用变量来存。
class Solution {public: vector<int> productExceptSelf(vector<int>& nums) { int n = nums.size(); vector<int> ret(n, 1); for (int i = 1; i < n; i++) ret[i] = ret[i-1] * nums[i-1]; long long right = 1; for(int i = n - 1; i >= 0; i--) { ret[i] *= right; right *= nums[i]; } return ret; }};
阅读全文
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
- 在ArcMap 10.2中进行了坐标系定义和投影转换后,若干图层在放大到某一比例尺时不再显示的问题
- 大数据学习
- Hadoop与Spark算法分析(二)——排序算法
- Mac安装Mysql及安装mysql-connector驱动
- TDDL集成springMVC的配置
- leetcode 238. Product of Array Except Self
- 第三章 线性模型--机器学习(周志华)参考答案
- nodeJS基础——JavaScript模块开发
- Redis学习笔记之十:Redis用作消息队列
- Socket常见错误代码与描述
- 2.2 不要被阶乘吓跑
- andriodjie——实现小圆点
- 音频配置 & iPhone X的适配
- Android Studio项目整合PullToRefresh的问题记录