- 主要数据结构:
array,map,set - 优点:查询时间复杂度为
O(1),以空间换时间 - 什么时候使用哈希法:当需要查询一个元素是否出现过,或者一个元素是否在集合里的时候
map 简洁赋值方法:map.set(i, (map.get(i) ?? 0) + 1)
leetcode:
- 1. 两数之和 - 力扣(LeetCode)
- 290. 单词规律 - 力扣(LeetCode)
- 242. 有效的字母异位词 - 力扣(LeetCode)
- 49. 字母异位词分组 - 力扣(LeetCode)
- 202. 快乐数 - 力扣(LeetCode)
- 128. 最长连续序列 - 力扣(LeetCode)
202 题,其中计算一个数的各位相加算法:
function calculate(sum) {
let res = 0
while (sum) {
res += (sum % 10)
sum = Math.floor(sum / 10)
}
return res
}