diff --git a/LeetCode/Product_of_Array_Except_Self.cpp b/LeetCode/Product_of_Array_Except_Self.cpp new file mode 100644 index 0000000..f2a35d8 --- /dev/null +++ b/LeetCode/Product_of_Array_Except_Self.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + vector productExceptSelf(vector& nums) { + int n = nums.size(); + vector result(n, 1); + + int left = 1; + for (int i = 0; i < n; i++) { + result[i] *= left; + left *= nums[i]; + } + + int right = 1; + for (int i = n - 1; i >= 0; i--) { + result[i] *= right; + right *= nums[i]; + } + + return result; + } +}; \ No newline at end of file