-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmaximumProductOfTwoElementsInAnArray_Dream.java
49 lines (45 loc) · 1.55 KB
/
maximumProductOfTwoElementsInAnArray_Dream.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
import java.util.*;
import java.util.stream.Collectors;
/**
* @author Dream
*/
public class maximumProductOfTwoElementsInAnArray_Dream {
//研究了arrays.sort方法写的
class Solution1 {
public int maxProduct(int[] nums) {
for (int i = 0, j = i; i < nums.length - 1; j = ++i) {
int ai = nums[i + 1];
while (nums[j] < ai) {
nums[j + 1] = nums[j];
if (j-- == 0) {
break;
}
}
nums[j + 1] = ai;
}
return ((nums[0] - 1) * (nums[1] - 1));
}
}
//两次遍历取出最大值,并删除
class Solution2 {
public int maxProduct(int[] nums) {
Map<Integer, Integer> map = new HashMap<>(2);
map.put(0,0);
map.put(1,0);
List<Integer> collect = Arrays.stream(nums).boxed().collect(Collectors.toList());
foreachListToRemove(collect, map, 0);
foreachListToRemove(collect, map, 1);
return (map.get(0) - 1) * (map.get(1) - 1);
}
public void foreachListToRemove(List<Integer> collection,Map<Integer,Integer> map,Integer index) {
Iterator<Integer> iterator = collection.iterator();
while (iterator.hasNext()) {
Integer value = iterator.next();
if (map.get(index) < value) {
map.put(index, value);
}
}
collection.remove(map.get(index));
}
}
}