版权声明:本人原创,转载请注明来源! https://blog.csdn.net/KAIKAI_ING/article/details/82943344
两类查找问题
- 查找有无:元素"a"是否存在? 使用set;集合
- 查找对应关系(键值对应):元素"a"出现了几次? map;字典
set和map
- 通常语言的标准库中都有内置set和map
- 容器类
- 屏蔽实现细节
- 了解语言中标准库里常见的容器类的使用
set和map常见操作
- insert
- find
- erase
- chang(map)
4-1 set的使用 Intersection of Two Arrays
题目: LeetCode 349. 两个数组的交集
给定两个数组,编写一个函数来计算它们的交集。
示例 1:
输入: nums1 = [1,2,2,1], nums2 = [2,2]
输出: [2]
示例 2:
输入: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
输出: [9,4]
说明:
输出结果中的每个元素一定是唯一的。
我们可以不考虑输出结果的顺序。
import java.util.TreeSet;
// 349. Intersection of Two Arrays
// https://leetcode.com/problems/intersection-of-two-arrays/description/
// 时间复杂度: O(nlogn)
// 空间复杂度: O(n)
public class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
TreeSet<Integer> record = new TreeSet<Integer>();
for(int num: nums1)
record.add(num);
TreeSet<Integer> resultSet = new TreeSet<Integer>();
for(int num: nums2)
if(record.contains(num))
resultSet.add(num);
int[] res = new int[resultSet.size()];
int index = 0;
for(Integer num: resultSet)
res[index++] = num;
return res;
}
private static void printArr(int[] arr){
for(int e: arr)
System.out.print(e + " ");
System.out.println();
}
public static void main(String[] args) {
int[] nums1 = {1, 2, 2, 1};
int[] nums2 = {2, 2};
int[] res = (new Solution()).intersection(nums1, nums2);
printArr(res);
}
}
4-2 map的使用 Intersection of Two Arrays II
题目: LeetCode 350. 两个数组的交集 II
给定两个数组,编写一个函数来计算它们的交集。
示例 1:
输入: nums1 = [1,2,2,1], nums2 = [2,2]
输出: [2,2]
示例 2:
输入: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
输出: [4,9]
说明:
输出结果中每个元素出现的次数,应与元素在两个数组中出现的次数一致。
我们可以不考虑输出结果的顺序。
进阶:
如果给定的数组已经排好序呢?你将如何优化你的算法?
如果 nums1 的大小比 nums2 小很多,哪种方法更优?
如果 nums2 的元素存储在磁盘上,磁盘内存是有限的,并且你不能一次加载所有的元素到内存中,你该怎么办?
import java.util.TreeMap;
import java.util.ArrayList;
// 350. Intersection of Two Arrays II
// https://leetcode.com/problems/intersection-of-two-arrays-ii/description/
// 时间复杂度: O(nlogn)
// 空间复杂度: O(n)
public class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
TreeMap<Integer, Integer> record = new TreeMap<Integer, Integer>();
for(int num: nums1)
if(!record.containsKey(num))
record.put(num, 1);
else
record.put(num, record.get(num) + 1);
ArrayList<Integer> result = new ArrayList<Integer>();
for(int num: nums2)
if(record.containsKey(num) && record.get(num) > 0){
result.add(num);
record.put(num, record.get(num) - 1);
}
int[] ret = new int[result.size()];
int index = 0;
for(Integer num: result)
ret[index++] = num;
return ret;
}
private static void printArr(int[] arr){
for(int e: arr)
System.out.print(e + " ");
System.out.println();
}
public static void main(String[] args) {
int[] nums1 = {1, 2, 2, 1};
int[] nums2 = {2, 2};
int[] res = (new Solution()).intersect(nums1, nums2);
printArr(res);
}
}
/// 让我们来测试使用Java中的TreeMap:)
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
TreeMap<Integer, Integer> myMap = new TreeMap<Integer, Integer>();
if(myMap.containsKey(42))
System.out.println("Element 42 is in the map");
else
System.out.println("Can not find element 42");
System.out.println(myMap.get(42)); // 输出 null
// Java不存在C++中默认的访问key即添加默认(key, value)的行为
// 以下代码仍然无法找到42
if(myMap.containsKey(42))
System.out.println("Element 42 is in the map");
else
System.out.println("Can not find element 42");
myMap.put(42, 0);
myMap.put(42, myMap.get(42) + 1);
System.out.println(myMap.get(42)); // 输出 1
if(myMap.containsKey(42))
System.out.println("Element 42 is in the map");
else
System.out.println("Can not find element 42");
myMap.put(42, myMap.get(42) - 1);
System.out.println(myMap.get(42)); // 输出 0
// 注意: key对应的值为0, 不代表key不存在
if(myMap.containsKey(42))
System.out.println("Element 42 is in the map");
else
System.out.println("Can not find element 42");
// 注意: 也不可以为key对应的值设置null来删除一个key
myMap.put(42, null);
if(myMap.containsKey(42))
System.out.println("Element 42 is in the map");
else
System.out.println("Can not find element 42");
// 使用remove删除一个key
myMap.remove(42);
if(myMap.containsKey(42))
System.out.println("Element 42 is in the map");
else
System.out.println("Can not find element 42");
}
}