查找表
349. 两个数组的交集
class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
int[] more = nums1.length > nums2.length ? nums1 : nums2;
int[] less = nums1.length > nums2.length ? nums2 : nums1;
Set<Integer> set = new HashSet<>();
for (int item : more) {
set.add(item);
}
Set<Integer> result = new HashSet<>();
for (int item : less) {
if (set.contains(item)) {
result.add(item);
}
}
return result.stream().mapToInt(Integer::intValue).toArray();
}
}
350. 两个数组的交集II
class Solution {
// 如果数组有序,可以用双指针解题
public int[] intersect(int[] nums1, int[] nums2) {
int[] more = nums1.length > nums2.length ? nums1 : nums2;
int[] less = nums1.length > nums2.length ? nums2 : nums1;
Map<Integer, Integer> map = new HashMap<>();
for (int item : more) {
map.put(item, map.getOrDefault(item, 0) + 1);
}
List<Integer> result = new ArrayList<>();
for (int item : less) {
if (map.containsKey(item) && map.get(item) > 0) {
result.add(item);
map.put(item, map.get(item) - 1);
}
}
return result.stream().mapToInt(Integer::intValue).toArray();
}
}
242. 有效的字母异位词
class Solution {
public boolean isAnagram(String s, String t) {
if (s.length() != t.length()) {
return false;
}
int[] map = new int[26];
for (int i = 0; i < s.length(); i++) {
map[s.codePointAt(i) - 97]++;
map[t.codePointAt(i) - 97]--;
}
for (int item : map) {
if (item != 0) {
return false;
}
}
return true;
}
}
1. 两数之和
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer,Integer> map = new HashMap<>();
for(int i=0; i<nums.length; i++){
int ano = target - nums[i];
if(map.containsKey(ano)){
int idx = map.get(ano);
return new int[]{idx, i};
}
map.put(nums[i], i);
}
return null;
}
}
454. 四数相加II
class Solution {
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
Map<Integer, Integer> sumCount = new HashMap<>();
for (int e1 : nums1) {
for (int e2 : nums2) {
sumCount.merge(e1 + e2, 1, Integer::sum);
}
}
int count = 0;
for (int e3 : nums3) {
for (int e4 : nums4) {
int sum = e3 + e4;
if (sumCount.containsKey(-sum)) {
count += sumCount.get(-sum);
}
}
}
return count;
}
}
447. 回旋镖的数量
class Solution {
public int numberOfBoomerangs(int[][] points) {
int result = 0;
Map<Integer, Integer> distCount = new HashMap<>();
for (int i = 0; i < points.length; i++) {
distCount.clear();
for (int j = 0; j < points.length; j++) {
if (i != j) {
distCount.merge(distSquare(points[i], points[j]), 1, Integer::sum);
}
}
for (Integer count : distCount.values()) {
result += count * (count - 1);
}
}
return result;
}
private Integer distSquare(int[] p1, int[] p2) {
return (p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]);
}
}
219. 存在重复元素II
class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
if (k == 0) {
return false;
}
Set<Integer> window = new HashSet<>();
for (int i = 0; i < nums.length; i++) {
if (!window.add(nums[i])) {
return true;
}
if (window.size() > k) {
window.remove(nums[i - k]);
}
}
return false;
}
}
220. 存在重复元素III
class Solution {
public boolean containsNearbyAlmostDuplicate(int[] nums, int indexDiff, int valueDiff) {
TreeSet<Integer> ts = new TreeSet<>();
for (int i = 0; i < nums.length; i++) {
int current = nums[i];
Integer ceil = ts.ceiling(current);
Integer floor = ts.floor(current);
if ((ceil != null && ceil - current <= valueDiff)
|| (floor != null && current - floor <= valueDiff)) {
return true;
}
ts.add(current);
if (i >= indexDiff) {
ts.remove(nums[i - indexDiff]);
}
}
return false;
}
}
License:
CC BY 4.0