-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
152.java
55 lines (46 loc) · 1.5 KB
/
152.java
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
__________________________________________________________________________________________________
sample 0 ms submission
class Solution {
public int maxProduct(int[] nums) {
if (nums.length == 0) {
return 0;
}
int max = Integer.MIN_VALUE;
int product = 1;
for (int i=0; i<nums.length; i++) {
product *= nums[i];
max = Math.max(max, product);
if (product == 0) {
product = 1;
}
}
product = 1;
for (int i=nums.length-1; i>=0; i--) {
product *= nums[i];
max = Math.max(max, product);
if (product == 0) {
product = 1;
}
}
return max;
}
}
__________________________________________________________________________________________________
sample 34212 kb submission
class Solution {
public int maxProduct(int[] nums) {
if(nums == null || nums.length == 0) return 0;
int max = nums[0];
int min = nums[0];
int result = nums[0];
for(int i = 1; i<nums.length; i++){
int a = max*nums[i];
int b = min*nums[i];
max = Math.max(Math.max(a,b), nums[i]);
min = Math.min(Math.min(a,b), nums[i]);
result = Math.max(result, max);
}
return result;
}
}
__________________________________________________________________________________________________