美文网首页
算法-HashMap哈希表题型

算法-HashMap哈希表题型

作者: 码农朱同学 | 来源:发表于2022-08-09 17:52 被阅读0次

HashMap 哈希表

  • HashMap
    • 用Array+LinkedList实现的能在平均O(1)时间内快速增删查的数据结构
    • 表内存储的数据需要实现equals()和hashCode()
  • LinkedHashMap
    • 有顺序的hashmap,遍历顺序是key插入的顺序
    • 所以的key按顺序存成一个LinkedList
  • TreeMap
    • 有顺序的map,遍历顺序是key从小到大
    • 所以的key存成一个红黑树
    • 增删查O(logN)
  • HashSet
    • 没有value的HashMap

当需要快速查找数据时,可以利用HashMap加速查找(注意要查找的数据结构实现了equal()和hashCode())

实例


/*
  给定一个整数数组 nums和一个目标值 target,请你在该数组中找出和为目标值的那两个整数,并返回他们的数组下标。

  你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。

  
  示例:

  给定 nums = [2, 7, 11, 15], target = 9

  因为 nums[0] + nums[1] = 2 + 7 = 9
  所以返回 [0, 1]

 */


public class LeetCode01 {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums = {7, 2, 12, 8, 15};
        int target = 14;
        System.out.print(Arrays.toString(solution.twoSum3(nums, target)));
    }

    static class Solution {
        public int[] twoSum1(int[] nums, int target) {
            for (int i = 0; i < nums.length; i++) {
                for (int j = i + 1; j < nums.length; j++) {
                    if (nums[j] == target - nums[i]) {
                        return new int[]{i, j};
                    }
                }
            }
            throw new IllegalArgumentException("No two sum solution");
        }

        public int[] twoSumHash(int[] nums, int target) {
            Map<Integer, Integer> map = new HashMap<>();
            for (int i = 0; i < nums.length; i++) {
                map.put(nums[i], i);
            }
            Iterator it = map.keySet().iterator();
            int key, value;
            while (it.hasNext()) {
                key = (int) it.next();
                value = map.get(key);
                int left = target - key;
                if (map.containsKey(left) && value != map.get(left)) {
                    return new int[]{value, map.get(left)};
                }
            }
            throw new IllegalArgumentException("No two sum solution");
        }

        public int[] twoSum2(int[] nums, int target) {
            Map<Integer, Integer> map = new HashMap<>();
            for (int i = 0; i < nums.length; i++) {
                map.put(nums[i], i);
            }
            for (int i = 0; i < nums.length; i++) {
                int left = target - nums[i];
                if (map.containsKey(left) && map.get(left) != i) {
                    return new int[]{i, map.get(left)};
                }
            }
            throw new IllegalArgumentException("No two sum solution");
        }

        public int[] twoSum3(int[] nums, int target) {
            Map<Integer, Integer> map = new HashMap<>();
            for (int i = 0; i < nums.length; i++) {
                int left = target - nums[i];
                if (map.containsKey(left)) {
                    return new int[]{map.get(left), i};
                }
                map.put(nums[i], i);
            }
            throw new IllegalArgumentException("No two sum solution");
        }
    }
}

相关文章

网友评论

      本文标题:算法-HashMap哈希表题型

      本文链接:https://www.haomeiwen.com/subject/dhinwrtx.html