Skip to content

Commit f2cb0ee

Browse files
authored
Added cpp solution for 238
1 parent c8a98ac commit f2cb0ee

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

leetcode/cpp/Array/238.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
vector<int> productExceptSelf(vector<int>& nums) {
4+
int n = nums.size();
5+
vector<int> ans(n);
6+
7+
ans[0] = 1;
8+
for (int i = 1; i < n; i++) {
9+
ans[i] = nums[i - 1] * ans[i - 1];
10+
}
11+
12+
int right = 1;
13+
for (int i = n - 1; i >= 0; i--) {
14+
ans[i] = ans[i] * right;
15+
right *= nums[i];
16+
}
17+
18+
return ans;
19+
}
20+
};

0 commit comments

Comments
 (0)