1、基础知识
HashSet
变量和类型 | 方法 | 描述 |
---|---|---|
boolean | add(E e) | 如果指定的元素尚不存在,则将其添加到此集合中。 |
void | clear() | 从该集中删除所有元素。 |
Object | clone() | 返回此 |
boolean | contains(Object o) | 如果此set包含指定的元素,则返回 |
boolean | isEmpty() | 如果此集合不包含任何元素,则返回 |
Iterator<E> | iterator() | 返回此set中元素的迭代器。 |
boolean | remove(Object o) | 如果存在,则从该集合中移除指定的元素。 |
int | size() | 返回此集合中的元素数(基数)。 |
Spliterator<E> | spliterator() | 在此集合中的元素上创建late-binding和失败快速 Spliterator 。 |
HashSet add contains remove size
HashMap
方法 | 描述 |
---|---|
clear() | 删除 hashMap 中的所有键/值对 |
clone() | 复制一份 hashMap |
isEmpty() | 判断 hashMap 是否为空 |
size() | 计算 hashMap 中键/值对的数量 |
put() | 将键/值对添加到 hashMap 中 |
putAll() | 将所有键/值对添加到 hashMap 中 |
putIfAbsent() | 如果 hashMap 中不存在指定的键,则将指定的键/值对插入到 hashMap 中。 |
remove() | 删除 hashMap 中指定键 key 的映射关系 |
containsKey() | 检查 hashMap 中是否存在指定的 key 对应的映射关系。 |
containsValue() | 检查 hashMap 中是否存在指定的 value 对应的映射关系。 |
replace() | 替换 hashMap 中是指定的 key 对应的 value。 |
replaceAll() | 将 hashMap 中的所有映射关系替换成给定的函数所执行的结果。 |
get() | 获取指定 key 对应对 value |
getOrDefault() | 获取指定 key 对应对 value,如果找不到 key ,则返回设置的默认值 |
forEach() | 对 hashMap 中的每个映射执行指定的操作。 |
entrySet() | 返回 hashMap 中所有映射项的集合集合视图。 |
keySet() | 返回 hashMap 中所有 key 组成的集合视图。 |
values() | 返回 hashMap 中存在的所有 value 值。 |
merge() | 添加键值对到 hashMap 中 |
compute() | 对 hashMap 中指定 key 的值进行重新计算 |
computeIfAbsent() | 对 hashMap 中指定 key 的值进行重新计算,如果不存在这个 key,则添加到 hasMap 中 |
computeIfPresent() | 对 hashMap 中指定 key 的值进行重新计算,前提是该 key 存在于 hashMap 中。 |
HashMap containsKey get getOrDefault put putIfAbsent remove replace size
如何设计哈希表+原理
Map.Entry<Integer, Integer>也可以作为一个对象
2、基本题型
(1)设计相关数据结构
哈希+动态数组
剑指 Offer II 030. 插入、删除和随机访问都是 O(1) 的容器
HashMap:num->index
ArrayList:nums
Random random = new Random(); int r = random.nextInt(range);
哈希+链表(LRU)
1、定义一个双向链表节点ListNode class,key、value、next、prev
2、LRU:head、tail、HashMap<Integer, ListNode>、capacity
3、拆解三个方法:deleteNode(node)、insertNode(node) -> moveTotail(node, value)
(2)字符串-变位词
数组作为哈希【小写字母26个, ASCII256】
法一:数组作为哈希表,计数,比较(Arrays.equals(int[] a, int[] b))
法二:先排序,再比较(nlog(n))
先对单个字符串进行字符级别排序,然后转成哈希表
返回值可以new LinkedList<>(map.values())
(3)其他
1、用数组【26】建立哈希 字典
2、遍历,前后两两比较
比较函数
private boolean isSorted(String word1, String word2, int[] dict) {
int i = 0;
for (; i < word1.length() && i < word2.length(); ++i) {
char ch1 = word1.charAt(i);
char ch2 = word2.charAt(i);
if (dict[ch1 - 'a'] < dict[ch2 - 'a']) {
return true;
} else if (dict[ch1 - 'a'] > dict[ch2 - 'a']) {
return false;
}
}
return i == word1.length();//!!!!!妙啊
}
class Solution {
public int findMinDifference(List<String> timePoints) {
if(timePoints.size() > 1440) {
return 0;
}
boolean[] flags = new boolean[1440];//24*60
for (String timePoint: timePoints) {
String[] t = timePoint.split(":");
int min = Integer.parseInt(t[0]) * 60 + Integer.parseInt(t[1]);
if (flags[min]) {
return 0;
}
flags[min] = true;
}
int first = 1440;
int last = -1;
int prev = -1;
int minDiff = 720;
for (int i = 0; i < 1440; i++) {
if (flags[i]) {
if (prev >= 0) {
minDiff = Math.min(minDiff, i - prev);
}
prev = i;
first = Math.min(first, i);
last = Math.max(last, i);
}
}
return Math.min(first + 1440 - last, minDiff);
}
}