Skip to content

Commit ec77e43

Browse files
committed
remove duplicate
1 parent d1c27bd commit ec77e43

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

array/MaxProduct.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
public class MaxProduct {
44
public static int maxProduct(int[] A) {
55
int max = 0;
6-
7-
int left = 0;
8-
int right = 0;
9-
6+
107
int len = A.length;
118

129
int product = 1;
@@ -15,11 +12,13 @@ public static int maxProduct(int[] A) {
1512
if (A[i] <= 0) {
1613
max = Math.max(max, product);
1714
product = 1;
15+
continue;
1816
}
17+
1918
product *= A[i];
2019
}
2120

22-
return 0;
21+
return max;
2322
}
2423

2524
/*

array/RemoveDuplicates.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package Algorithms.array;
2+
3+
public class RemoveDuplicates {
4+
public int removeDuplicates(int[] A) {
5+
if (A == null || A.length == 0) {
6+
return 0;
7+
}
8+
9+
// A里至少有1个元素
10+
int len = 1;
11+
12+
for (int i = 1; i < A.length; i++) {
13+
if (A[i] != A[i - 1]) {
14+
A[len++] = A[i];
15+
}
16+
}
17+
18+
return len;
19+
}
20+
}

0 commit comments

Comments
 (0)