Skip to content

Commit f29e28f

Browse files
committed
Time: 9 ms (61.74%), Space: 3.1 MB (90.75%) - LeetHub
1 parent 666eca4 commit f29e28f

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
impl Solution {
2+
pub fn product_except_self(nums: Vec<i32>) -> Vec<i32>
3+
{
4+
let n = nums.len();
5+
let mut p =1;
6+
let mut zero = 0;
7+
let mut zeroi = 0usize;
8+
let mut ans = vec![0;n];
9+
10+
for i in 0..n
11+
{
12+
if(nums[i]==0)
13+
{
14+
zero+=1;
15+
zeroi = i;
16+
}else
17+
{
18+
//product of all element
19+
p=p*nums[i];
20+
}
21+
}
22+
if(zero>1)
23+
{
24+
//multiple zero, so all elements will become zero
25+
return ans;
26+
}
27+
else if(zero==1)
28+
{
29+
//if one zero position only that position have multiplied p
30+
ans[zeroi]=p;
31+
return ans;
32+
}
33+
for i in 0..n
34+
{
35+
//total multiplied value divided by current position
36+
ans[i]=p/nums[i];
37+
}
38+
return ans;
39+
}
40+
}

0 commit comments

Comments
 (0)