class Solution { public: vector<int> productExceptSelf(vector<int>& nums) { //用除法必须要考虑元素为0的情况,用双重循环要考虑数组过大超时的情况 //双指针双vector O(n)time O(n) space int n=nums.size(); vector<int> frombegin(n+1,0); vector<int> fromend(n+1,0); vector<int> output(n); frombegin[0]=1,fromend[n]=1; for(int i=1;i<=n;i++){ frombegin[i]=frombegin[i-1]*nums[i-1]; fromend[n-i]=fromend[n-i+1]*nums[n-i]; } for(int i=0;i<n;i++){ output[i]=frombegin[i]*fromend[i+1]; } return output; } };
原文地址:https://www.cnblogs.com/joelwang/p/10970708.html
时间: 2024-10-31 00:18:31