1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-11-10 00:09:06 +01:00

cs: add «238. Product of Array Except Self»

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2024-03-15 17:28:55 +01:00
parent 4763cdbdda
commit 15bd5cb743
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -0,0 +1,28 @@
public class Solution {
private int Get(int[] products, int idx) {
if (idx < 0 || idx >= products.Length) {
return 1;
}
return products[idx];
}
public int[] ProductExceptSelf(int[] nums) {
var left = new int[nums.Length];
var right = new int[nums.Length];
for (var i = 0; i < nums.Length; ++i) {
left[i] = nums[i] * Get(left, i - 1);
int j = nums.Length - i - 1;
right[j] = nums[j] * Get(right, j + 1);
}
var result = new int[nums.Length];
for (var i = 0; i < nums.Length; ++i) {
result[i] = Get(left, i - 1) * Get(right, i + 1);
}
return result;
}
}