Repository navigation
Expand file tree
/
Copy pathproblem238.java
More file actions
72 lines (56 loc) · 1.49 KB
/
Copy pathproblem238.java
File metadata and controls
72 lines (56 loc) · 1.49 KB
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
//238. Product of Array Except Self
public class problem238 {
public static int[] productExceptSelf(int[] nums) {
int[] ans = new int[nums.length];
for(int i=0;i<ans.length;i++)
{
ans[i]=0;
}
int c=0,p=1,tem=-1;
for(int i=0;i<nums.length;i++)
{
if(nums[i]!=0)
{
p*=nums[i];
}
if(nums[i]==0)
{
if(c==0)
{ c++;
tem=i;
continue;}
if(c>=1)
{
c++;
break;
}
}
}
if(c==1)
{
ans[tem]=p;
return ans;
}
if(c>1)
{
return ans;
}
if(c==0)
{
for(int i=0;i<nums.length;i++)
{
ans[i]=p/nums[i];
}
return ans;
}
return ans;
}
public static void main(String[] args) {
int [] a ={1,2,3,4,0};
int[] ans = productExceptSelf(a);
for(int i=0;i<ans.length;i++)
{
System.out.print(ans[i]+" ");
}
}
}