diff --git a/productExceptSelf_shadowkakashi.cpp b/productExceptSelf_shadowkakashi.cpp new file mode 100644 index 0000000..9a7a788 --- /dev/null +++ b/productExceptSelf_shadowkakashi.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + vector productExceptSelf(vector& nums) { + int n = nums.size(); + vector res(n, 1); + int prefix = 1, suffix = 1; + for (int i = 0; i < n; ++i) { + res[i] *= prefix; + prefix *= nums[i]; + } + for (int i = n - 1; i >= 0; --i) { + res[i] *= suffix; + suffix *= nums[i]; + } + return res; + } +};