Skip to content

Commit 68233ea

Browse files
committed
2주차 문제 풀이 1개 추가
- Product of Array Except Self
1 parent 1c6e705 commit 68233ea

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
{
10+
ans[i] = ans[i-1] * nums[i-1];
11+
}
12+
13+
int suffix = 1;
14+
for (int i = n-1; i >= 0; i--)
15+
{
16+
ans[i] *= suffix;
17+
suffix *= nums[i];
18+
}
19+
20+
return ans;
21+
}
22+
};

0 commit comments

Comments
 (0)