-
Notifications
You must be signed in to change notification settings - Fork 0
/
product_of_array_except_self.rs
77 lines (64 loc) · 1.82 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/// 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.
struct Solution;
impl Solution {
pub fn product_except_self(nums: Vec<i32>) -> Vec<i32> {
let n = nums.len();
let mut left = vec![0; n];
let mut right = vec![0; n];
let mut result = vec![0; n];
let mut product = 1;
left[0] = product;
for i in 0..n-1 {
let value = nums[i];
product *= value;
left[i+1] = product;
}
product = 1;
right[n-1] = product;
for i in 0..n-1 {
let value = nums[n-1-i];
product *= value;
right[n-i-2] = product;
}
for i in 0..n {
result[i] = left[i] * right[i];
}
result
}
}
#[cfg(test)]
mod tests {
use super::Solution;
#[test]
fn example_1() {
let nums = vec![1,2,3,4];
let result = Solution::product_except_self(nums);
assert_eq!(result, vec![24,12,8,6]);
}
#[test]
fn exmaple_2() {
let nums = vec![-1, 1, 0, -3, 3];
let result = Solution::product_except_self(nums);
assert_eq!(result, vec![0, 0, 9, 0, 0]);
}
#[test]
fn two_zeroes() {
let nums = vec![0, 1, 1, 0];
let result = Solution::product_except_self(nums);
assert_eq!(result, vec![0, 0, 0, 0]);
}
#[test]
fn two_nums() {
let nums = vec![-30, 30];
let result = Solution::product_except_self(nums);
assert_eq!(result, vec![30, -30]);
}
}