Skip to content

Commit 6cf0af6

Browse files
committed
feat: Add solution for LeetCode problem 238
1 parent 1b27cf3 commit 6cf0af6

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// 238. Product of Array Except Self
3+
// https://leetcode.com/problems/product-of-array-except-self/description/
4+
// Dale-Study
5+
//
6+
// Created by WhiteHyun on 2024/06/01.
7+
//
8+
9+
final class Solution {
10+
func productExceptSelf(_ nums: [Int]) -> [Int] {
11+
var answer: [Int] = .init(repeating: 1, count: nums.count)
12+
13+
var left_product = 1
14+
for i in nums.indices {
15+
answer[i] = left_product
16+
left_product *= nums[i]
17+
}
18+
19+
var right_product = 1
20+
for i in nums.indices.reversed() {
21+
answer[i] *= right_product
22+
right_product *= nums[i]
23+
}
24+
25+
return answer
26+
}
27+
}

0 commit comments

Comments
 (0)