[LeetCode]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.)
思路
对于i = 5 时 result[5] = (nums[0] * nums[1] * nums[2] * nums[3] * nums[4] ) * (nums[6] nums[7] * nums[8] * nums[9] * nums[10])
从上面开始看出对于第i个,我们只要知道它左边的连续乘积 和 它右边的连续乘积 就OK了。
代码
/*---------------------------------------
* 日期:2015-07-31
* 作者:SJF0115
* 题目: 238.Product of Array Except Self
* 网址:https://leetcode.com/problems/product-of-array-except-self/
* 结果:AC
* 来源:LeetCode
* 博客:
-----------------------------------------*/
#include <iostream>
#include <vector>
using namespace std;class Solution {
public:vector<int> productExceptSelf(vector<int>& nums) {int size = nums.size();vector<int> result(size,0);vector<int> left(size,0);vector<int> right(size,0);int leftNum = 1,rightNum = 1;// left[i] 为 nums[0]...nums[i-1]的连续乘积// right[i] 为 nums[i+1]...nums[size-1]的连续乘积for(int i = 0;i < size;++i){left[i] = leftNum;right[size-i-1] = rightNum;leftNum *= nums[i];rightNum *= nums[size-i-1];}//for// 计算不包括自己的所有乘积for(int i = 0;i < size;++i){result[i] = left[i] * right[i];}//forreturn result;}
};int main(){Solution s;vector<int> vec = {1,2};vector<int> result = s.productExceptSelf(vec);int size = result.size();for(int i = 0;i < size;++i){cout<<result[i]<<" ";}//forreturn 0;
}
运行时间
[LeetCode]238.Product of Array Except Self相关推荐
- leetcode 238. Product of Array Except Self | 238. 除自身以外数组的乘积(Java)
题目 https://leetcode.com/problems/product-of-array-except-self/ 题解 本题思路比较简单,先算一个原数组的总的乘积 p,然后对应除以每一个位 ...
- 238. Product of Array Except Self
题目来源: https://leetcode.com/problems/product-of-array-except-self/ 自我感觉难度/真实难度: 这道题曾经在哪里做过,但是还是写不出代码, ...
- 【数组递归构造二叉树】LeetCode 108. Convert Sorted Array to Binary Search Tree
LeetCode 108. Convert Sorted Array to Binary Search Tree Solution1:我的答案 构造二叉树利用递归 /*** Definition fo ...
- LeetCode Maximum Product Subarray(最大子数组乘积)
Find the contiguous subarray within an array (containing at least one number) which has the large ...
- leetcode 384. Shuffle an Array | 384. 打乱数组(Fisher-Yates洗牌算法)
题目 https://leetcode.com/problems/shuffle-an-array/ 题解 本代码参考了 JDK 源码 Collections.shuffle() 的实现,也可以看 官 ...
- LeetCode之Merge Sorted Array
1.问题 Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. No ...
- 【Leetcode】Shuffle an Array
题目链接:https://leetcode.com/problems/shuffle-an-array/ 题目: Shuffle a set of numbers without duplicates ...
- LeetCode: 108. Convert Sorted Array to Binary Search Tree
题目 Given an array where elements are sorted in ascending order, convert it to a height balanced BST. ...
- LeetCode 之 Merge Sorted Array(排序)
[问题描写叙述] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array ...
最新文章
- ZooKeeper配置中出现Error contacting service. It is probably not running
- Web纯前端“旭日图”实现元素周期表
- Objective-C:MRC(引用计数器)在OC内部的可变对象是适用的,不可变对象是不适用的(例如 NSString、NSArray等)...
- 如何对batch的数据求Gram矩阵
- 基于hsv的亮度调整算法_基于手绘工程图离线识别的预处理研究精品论文推荐
- 基于springboot2.x集成缓存注解及设置过期时间
- ie8 object param没有效果_如何用php实现分页效果
- 【Python】Sklearn线性回归模型预测波士顿房价并绘图
- UE3 贴图支持及设置
- IGMP协议——IP组播之组管理协议
- MAC 开发环境搭建及工具
- php分页类 seo,laravel 分页seo浅谈
- 坚果pro2刷Nitrogen OS安卓pie纯正原生
- 三体中的思想实验二:黑暗森林(上)
- python3爬取网易云歌曲,利用python3爬取网易云周杰伦所有专辑,歌曲,评论,并完成可视-站长资讯中心...
- python 抓取 实时公交_Python实现命令行监控北京实时公交之一
- Windows下SVN修改已经提交的日志
- dango之day01
- 最近发现有很多人一直在问苹果ID双重认证怎么关闭。
- 浅谈前端SPA(单页面应用)