Skip to content

Commit 5527f2b

Browse files
committed
product of array except self solution
1 parent a554bbd commit 5527f2b

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
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+
vector<int> ans;
5+
ans.resize(nums.size(), 1);
6+
int p = 1;
7+
int idx = nums.size()-1;
8+
for(auto it = nums.rbegin(); it != nums.rend(); it++, idx--) {
9+
ans[idx] *= p;
10+
p *= *it; // 마지막 건 그냥 무시
11+
}
12+
p = 1;
13+
idx = 0;
14+
for(auto it = nums.begin(); it != nums.end(); it++, idx++) {
15+
ans[idx] *= p;
16+
p *= *it;
17+
}
18+
return ans;
19+
}
20+
};

0 commit comments

Comments
 (0)