泉州网站开发,入驻天猫商城的条件和费用,王也头像高清帅气,设计网站架构https://leetcode.cn/problems/product-of-array-except-self/description/?envTypestudy-plan-v2envIdtop-interview-150 问题在于不使用除法并且空间复杂度为O(1)#xff0c;当第一次从头开始遍历时由于不知道后续数组元素是什么#xff0c;所以无法得到答案#xf…https://leetcode.cn/problems/product-of-array-except-self/description/?envTypestudy-plan-v2envIdtop-interview-150 问题在于不使用除法并且空间复杂度为O(1)当第一次从头开始遍历时由于不知道后续数组元素是什么所以无法得到答案而如果当知道一个后续数组元素后又回去更新答案的话无疑会提高时间复杂度。不妨这样看待如果我们已经遍历一次数组并且能够记录下足够的信息的话那么下次我们再次遍历数组时不就可以相对地知道后续元素的信息了吗。由此推广为了算法简单一些我们甚至可以遍历有限次获得足够的信息然后一次得到最终答案。 由这样的思路我们再看问题对于任何一个元素其除了自身以外的的元素的乘积由两个部分构成一个是它的前序元素乘积一个是后续元素乘积。前者可以通过正向的遍历得到后者通过反向遍历也可以得到由此答案就明了了
class Solution {
public:vectorint productExceptSelf(vectorint nums) {int len nums.size();vectorint answer(len);int answer_R[len],answer_L[len];answer_L[0]1,answer_R[len-1]1;for(int i1;ilen;i){answer_L[i]answer_L[i-1]*nums[i-1];}for(int ilen-2;i0;i--){answer_R[i]answer_R[i1]*nums[i1];}for(int i0;ilen;i){answer[i]answer_L[i]*answer_R[i];}return answer;}
};
同理其实我们不需要两个数组只需要一个中间变量记录后续乘积的过程就可以了这样可以减小空间复杂度
class Solution {
public:vectorint productExceptSelf(vectorint nums) {int len nums.size();vectorint answer(len);answer[0]1;for(int i1;ilen;i){answer[i]answer[i-1]*nums[i-1];}int tempnums[len-1];for(int ilen-2;i0;i--){answer[i]temp*answer[i];temptemp*nums[i];}return answer;}
}; 本文由博客一文多发平台 OpenWrite 发布