-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
78 additions
and
1 deletion.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
src/main/java/com/ximo/datastructuresinaction/leetcode/solution349/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.ximo.datastructuresinaction.leetcode.solution349; | ||
|
||
import java.util.Arrays; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* @author xikl | ||
* @date 2018/12/28 | ||
*/ | ||
public class Solution { | ||
|
||
|
||
public int[] intersection(int[] nums1, int[] nums2) { | ||
Set<Integer> set1 = Arrays.stream(nums1).boxed().collect(Collectors.toSet()); | ||
Set<Integer> set2 = Arrays.stream(nums2).boxed().collect(Collectors.toSet()); | ||
|
||
set1.retainAll(set2); | ||
|
||
|
||
return set1.stream().mapToInt(Integer::intValue).toArray(); | ||
} | ||
|
||
|
||
|
||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/com/ximo/datastructuresinaction/leetcode/solution350/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.ximo.datastructuresinaction.leetcode.solution350; | ||
|
||
|
||
import java.util.HashMap; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* @author xikl | ||
* @date 2018/12/28 | ||
*/ | ||
public class Solution { | ||
|
||
public int[] intersection(int[] nums1, int[] nums2) { | ||
Map<Integer, Integer> map = new HashMap<>(); | ||
|
||
for (int num : nums1) { | ||
// if (!map.containsKey(num)) { | ||
// map.put(num, 1); | ||
// } else { | ||
// map.put(num, map.get(num) + 1); | ||
// } | ||
|
||
map.compute(num, (key, value) -> value == null ? 1 : value + 1); | ||
} | ||
|
||
List<Integer> list = new LinkedList<>(); | ||
for (int num : nums2) { | ||
// if (map.containsKey(num)) { | ||
// list.add(num); | ||
// map.put(num, map.get(num) - 1); | ||
// if (map.get(num) == 0) { | ||
// map.remove(num); | ||
// } | ||
// } | ||
map.computeIfPresent(num, (key, value) -> { | ||
list.add(key); | ||
value = value - 1; | ||
if (value == 0) { | ||
map.remove(key); | ||
} | ||
return value; | ||
}); | ||
} | ||
|
||
return list.stream().mapToInt(Integer::intValue).toArray(); | ||
} | ||
|
||
|
||
} |
2 changes: 1 addition & 1 deletion
2
...action/leetcode/Solution804/Solution.java → ...action/leetcode/solution804/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters