Skip to content

Commit 9afbc0b

Browse files
authored
product of array except self solution
1 parent 437976f commit 9afbc0b

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
impl Solution {
2+
pub fn product_except_self(nums: Vec<i32>) -> Vec<i32> {
3+
let mut ans = vec![1; nums.len()];
4+
let mut a = 1;
5+
for i in (0..nums.len()).rev() {
6+
ans[i] = a * nums[i];
7+
a = ans[i];
8+
}
9+
a = 1;
10+
for i in 0..nums.len() {
11+
ans[i] = a * (if i == nums.len() - 1 {1} else {ans[i + 1]});
12+
a *= nums[i];
13+
}
14+
return ans
15+
}
16+
}

0 commit comments

Comments
 (0)