Map哈希映射

哈希映射是用于存储 (key, value) 键值对的一种实现。

使用哈希映射的第一个场景是,我们需要更多的信息,而不仅仅是键。然后通过哈希映射建立密钥与信息之间的映射关系

让我们来看一个例子:

给定一个整数数组,返回两个数字的索引,使它们相加得到特定目标。

在这个例子中,如果我们只想在有解决方案时返回 true,我们可以使用哈希集合来存储迭代数组时的所有值,并检查 target - current_value 是否在哈希集合中。

但是,我们被要求返回更多信息,这意味着我们不仅关心值,还关心索引。我们不仅需要存储数字作为键,还需要存储索引作为值。因此,我们应该使用哈希映射而不是哈希集合。

模板

  1. /*
  2. * Template for using hash map to find duplicates.
  3. * Replace ReturnType with the actual type of your return value.
  4. */
  5. ReturnType aggregateByKey_hashmap(List<Type>& keys) {
  6. // Replace Type and InfoType with actual type of your key and value
  7. Map<Type, InfoType> hashmap = new HashMap<>();
  8. for (Type key : keys) {
  9. if (hashmap.containsKey(key)) {
  10. if (hashmap.get(key) satisfies the requirement) {
  11. return needed_information;
  12. }
  13. }
  14. // Value can be any information you needed (e.g. index)
  15. hashmap.put(key, value);
  16. }
  17. return needed_information;
  18. }

两个数组的交集 II

题目描述:

(力扣链接🔗

Map哈希映射 - 图1

题目分析:

使用map来进行查重,此时选择map因为需要记录次数。也可以使用双指针法。

代码:

解法一:

  1. /**
  2. * hash表(使用map) 时间复杂度:O(m+n) 空间复杂度:O(min(m,n))
  3. * @param nums1
  4. * @param nums2
  5. * @return
  6. */
  7. public int[] intersect(int[] nums1, int[] nums2) {
  8. Map<Object, Integer> map = new HashMap<>();
  9. List<Integer> list = new ArrayList<>();
  10. // 将数字保存在map中,value存储次数
  11. for (int i = 0; i < nums2.length; i++) {
  12. map.put(nums2[i], map.getOrDefault(nums2[i], 0) + 1);
  13. }
  14. for (int i = 0; i < nums1.length; i++) {
  15. // map中存在且不为0
  16. if (map.containsKey(nums1[i]) && map.get(nums1[i]) != 0) {
  17. map.put(nums1[i], map.getOrDefault(nums1[i], 0) - 1);
  18. list.add(nums1[i]);
  19. }
  20. }
  21. // 转化为int数组
  22. int[] ints = new int[list.size()];
  23. int index = 0;
  24. for(Integer value : list) {
  25. ints[index++] = value;
  26. }
  27. return ints;
  28. }

解法二:

思路为2个数组分别使用2个指针进行遍历,在判断是否相等,不相等则移动指针。

  1. /**
  2. * 排序 + 双指针
  3. *
  4. * @param nums1
  5. * @param nums2
  6. * @return
  7. */
  8. public int[] intersect(int[] nums1, int[] nums2) {
  9. // 将其进行排序,分别使用2个指针进行遍历
  10. Arrays.sort(nums1);
  11. Arrays.sort(nums2);
  12. int index = 0, index1 = 0, index2 = 0;
  13. int[] ints = new int[Math.min(nums1.length, nums2.length)]; // 用来保存交集
  14. // 2个指针一直向后判断,直到其中一个指针遍历完毕
  15. while (index1 < nums1.length && index2 < nums2.length) {
  16. // 谁小就向后移动
  17. if (nums1[index1] < nums2[index2]) {
  18. index1++;
  19. } else if (nums1[index1] > nums2[index2]) {
  20. index2++;
  21. } else { // 相等赋值进去即可
  22. ints[index] = nums1[index1];
  23. index++;
  24. index1++;
  25. index2++;
  26. }
  27. }
  28. // 将后面为0的截取掉
  29. return Arrays.copyOfRange(ints, 0, index);
  30. }

字母异位词

题目描述

力扣链接🔗

Map哈希映射 - 图2

题目分析:

可以知道每个字符串字母都一样,但是顺序不同,可以先将字符串进行排序,在放到map中查找是否存在重复。此时map中的value不需要存储次数或者索引,所以我们可以使用value来存储复合条件的字符串集合。

代码:

  1. class Solution {
  2. /**
  3. * map解法
  4. * @param strs
  5. * @return
  6. */
  7. public List<List<String>> groupAnagrams(String[] strs) {
  8. HashMap<Object, List<String>> map = new HashMap<>();
  9. List<List<String>> list = new ArrayList<>();
  10. for (int i = 0; i < strs.length; i++) {
  11. String str = sort(strs[i]);
  12. if (map.containsKey(str)) {
  13. map.get(str).add(strs[i]);
  14. } else {
  15. List<String> stringList = new ArrayList<>();
  16. stringList.add(strs[i]);
  17. map.put(str, stringList);
  18. }
  19. }
  20. // 遍历将map中的value全部取出来
  21. for (Map.Entry<Object, List<String>> entry : map.entrySet()) {
  22. list.add(entry.getValue());
  23. }
  24. return list;
  25. }
  26. // 将其进行排序
  27. public String sort(String str) {
  28. char[] record = str.toCharArray();
  29. Arrays.sort(record);
  30. return String.valueOf(record);
  31. }
  32. }

两数之和

题目描述:

力扣链接🔗

Map哈希映射 - 图3

题目分析:

  • 数组的大小是受限制的,而且如果元素很少,而哈希值太大会造成内存空间的浪费。
  • set是一个集合,里面放的元素只能是一个key,而两数之和这道题目,不仅要判断y是否存在而且还要记录y的下标位置,因为要返回x 和 y的下标。所以set 也不能用。

使用map可以存储索引值。

代码:

  1. class Solution {
  2. public int[] twoSum(int[] nums, int target) {
  3. Map<Integer, Integer> map = new HashMap<>();
  4. for (int i = 0; i < nums.length; i++) {
  5. // 相减后,判断后边是否存在temp即可
  6. int temp = target - nums[i];
  7. if (map.containsKey(temp)) {
  8. return new int[]{map.get(temp), i};
  9. }
  10. // 将索引作为value
  11. map.put(nums[i], i);
  12. }
  13. return new int[]{};
  14. }
  15. }

四数相加II

题目描述:

力扣链接🔗

Map哈希映射 - 图4

题目分析:

  1. 首先定义 一个unordered_map,key放a和b两数之和,value 放a和b两数之和出现的次数。
  2. 遍历大A和大B数组,统计两个数组元素之和,和出现的次数,放到map中。
  3. 定义int变量count,用来统计 a+b+c+d = 0 出现的次数。
  4. 在遍历大C和大D数组,找到如果 0-(c+d) 在map中出现过的话,就用count把map中key对应的value也就是出现次数统计出来。
  5. 最后返回统计值 count 就可以了

巧妙的通过先计算2个数组中所有总和,并使用map来记录次数,在判断相加是否为0时,将0减去相加的数,即可转化为查重。

代码:

  1. class Solution {
  2. public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
  3. Map<Integer, Integer> map = new HashMap<>();
  4. int total = 0;
  5. for (int num1 : nums1) {
  6. for (int num2 : nums2) {
  7. // 将2数之和放入map,value保存出现的次数
  8. int temp = num1 + num2;
  9. map.put(temp, map.getOrDefault(temp, 0) + 1);
  10. }
  11. }
  12. for (int num3 : nums3) {
  13. for (int num4 : nums4) {
  14. // 在man中找相加等于0的数
  15. int temp = 0 - (num3 + num4);
  16. if (map.containsKey(temp)) {
  17. total += map.get(temp);
  18. }
  19. }
  20. }
  21. return total;
  22. }
  23. }