Skip to content

Commit c393a13

Browse files
author
Dream
committed
[update]
1 parent 9512c35 commit c393a13

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import java.util.*;
2+
import java.util.stream.Collectors;
3+
4+
/**
5+
* @author Dream
6+
*/
7+
public class maximumProductOfTwoElementsInAnArray_Dream {
8+
//研究了arrays.sort方法写的
9+
class Solution1 {
10+
public int maxProduct(int[] nums) {
11+
for (int i = 0, j = i; i < nums.length - 1; j = ++i) {
12+
int ai = nums[i + 1];
13+
while (nums[j] < ai) {
14+
nums[j + 1] = nums[j];
15+
if (j-- == 0) {
16+
break;
17+
}
18+
}
19+
nums[j + 1] = ai;
20+
}
21+
return ((nums[0] - 1) * (nums[1] - 1));
22+
}
23+
}
24+
25+
//两次遍历取出最大值,并删除
26+
class Solution2 {
27+
public int maxProduct(int[] nums) {
28+
Map<Integer, Integer> map = new HashMap<>(2);
29+
map.put(0,0);
30+
map.put(1,0);
31+
List<Integer> collect = Arrays.stream(nums).boxed().collect(Collectors.toList());
32+
foreachListToRemove(collect, map, 0);
33+
foreachListToRemove(collect, map, 1);
34+
return (map.get(0) - 1) * (map.get(1) - 1);
35+
}
36+
37+
public void foreachListToRemove(List<Integer> collection,Map<Integer,Integer> map,Integer index) {
38+
Iterator<Integer> iterator = collection.iterator();
39+
while (iterator.hasNext()) {
40+
Integer value = iterator.next();
41+
if (map.get(index) < value) {
42+
map.put(index, value);
43+
}
44+
}
45+
collection.remove(map.get(index));
46+
}
47+
}
48+
49+
}

algorithms/uniqueNumberOfOccurrences/uniqueNumberOfOccurrences.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
13

2-
class Solution {
4+
/**
5+
* @author Dream
6+
*/
7+
public class uniqueNumberOfOccurrences {
38
public boolean uniqueOccurrences(int[] arr) {
49
Map<Integer, Integer> map = new HashMap(arr.length);
510
for(int i : arr){
@@ -15,4 +20,5 @@ public boolean uniqueOccurrences(int[] arr) {
1520
}
1621
return true;
1722
}
18-
}
23+
24+
}

0 commit comments

Comments
 (0)