-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.rs
More file actions
33 lines (29 loc) · 703 Bytes
/
main.rs
File metadata and controls
33 lines (29 loc) · 703 Bytes
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
fn main() {
assert_eq!(Solution::product_except_self(vec![1,2,3,4]), [24,12,8,6]);
}
struct Solution {}
impl Solution {
/// use the output vector to achieve O(n) space
pub fn product_except_self(nums: Vec<i32>) -> Vec<i32> {
let mut res = vec![1];
let mut x = 1;
for i in 0..nums.len() - 1 {
x *= nums[i];
res.push(x);
}
let mut x = 1;
for i in (0..nums.len()).rev() {
res[i] = res[i] * x;
x *= nums[i];
}
res
}
}
#[cfg(test)]
mod test {
use crate::*;
#[test]
fn basic() {
assert_eq!(Solution::product_except_self(vec![1,2,3,4]), [24,12,8,6]);
}
}