Product of Array except self | leetcode
来源:互联网 发布:矩阵测光 编辑:程序博客网 时间:2024/06/06 09:33
思路
simple的方法就是使用除法,先得出整个数组的元素乘积(注意越界),然后每位除以该位元素,就能够得到结果。但是题目要求是不能使用除法,而且复杂度要求为O(N). 那么我们可以这么做,先从头到尾扫一边,得到前面所有元素的乘积,第一位设置为1,这样,最后一个数是满足题意的。其他还需要继续相乘,所以我们这次从尾开始扫一边,这样就能得到题的结果了。下面是实现代码:
vector<int> productOfArray(vector<int>& nums){ int n=nums.size(); vector<int> res(n,1); for(int i=1;i<n;++i) res[i]=res[i-1]*nums[i-1]; int right=1; for(int i=n-1;i>=0;--i){ res[i]*=right; right*=nums[i]; } return res;}
0 0
- [leetcode][array] Product of Array Except Self
- 【LeetCode】237 Product of Array Except Self
- #leetcode#Product of Array Except Self
- leetcode 238: Product of Array Except Self
- [leetcode] 238.Product of Array Except Self
- [leetcode] Product of Array Except Self
- LeetCode Product of Array Except Self
- leetCode(43):Product of Array Except Self
- LeetCode Product of Array Except Self
- 【leetcode】Product of Array Except Self
- 【leetcode】Product of Array Except Self
- leetcode: Product of Array Except Self
- [leetcode] Product of Array Except Self
- [LeetCode] Product of Array Except Self
- Leetcode: Product of Array Except Self
- LeetCode 238: Product of Array Except Self
- LeetCode-Product of Array Except Self
- Leetcode #238 Product of Array Except Self
- 微软面试100题-69
- UVA_11380_Down Went The Titanic
- 探究Lucene计算权重的过程
- jquery 中的 $(“#”) 与 js中的document.getElementById(“”) 的区别
- 数字在计算机中是如何存储的
- Product of Array except self | leetcode
- uCos-II内存管理
- AWS attach additional disk
- hdu_1532_Drainage Ditches
- Oracle 查询锁之间的依赖关系
- JS,Jquery获取各种屏幕的宽度和高度
- ubuntu 下配置Source Code Pro字体
- 11. Container With Most Water
- 理解dropout