0238.cpp (395B)
1 class Solution { 2 public: 3 vector<int> productExceptSelf(vector<int> &nums) { 4 int n = nums.size(); 5 vector<int> answer(n, 1); 6 7 int acc1 = 1, acc2 = 1; 8 for (int i = 0, j = n - 1; i < n; i++, j--) { 9 answer[i] *= acc1; 10 answer[j] *= acc2; 11 acc1 *= nums[i]; 12 acc2 *= nums[j]; 13 } 14 15 return answer; 16 } 17 };