-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproduct_of_array_except_self.rs
49 lines (40 loc) · 1.37 KB
/
product_of_array_except_self.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// # Product of Array Except Self
//
// Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].
//
// The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.
//
// You must write an algorithm that runs in O(n) time and without using the division operation.
pub fn solution(nums: Vec<i32>) -> Vec<i32> {
let nums_size = nums.len();
let mut prefix = vec![0; nums_size];
let mut suffix = vec![0; nums_size];
let mut answer = vec![1; nums_size];
prefix[0] = 1;
suffix[nums_size - 1] = 1;
for idx in 1..nums_size {
let prefix_index = idx;
let suffix_index = nums_size - 1 - idx;
prefix[prefix_index] = prefix[prefix_index - 1] * nums[prefix_index - 1];
suffix[suffix_index] = suffix[suffix_index + 1] * nums[suffix_index + 1];
answer[prefix_index] *= prefix[prefix_index];
answer[suffix_index] *= suffix[suffix_index];
}
answer
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn example_1() {
let nums = vec![1, 2, 3, 4];
let result = solution(nums);
assert_eq!(result, vec![24, 12, 8, 6]);
}
#[test]
fn example_2() {
let nums = vec![-1, 1, 0, -3, 3];
let result = solution(nums);
assert_eq!(result, vec![0, 0, 9, 0, 0]);
}
}