// 时间复杂赋O(m)+O(n) 空间复杂度O(m)varsection=function(nums1, nums2){const map =newMap()
nums1.forEach(n=>{
map.set(m,true)})const res =[]
nums2.forEacn(n=>{if(map.get(n)){
res.push(n)
map.delete(n)}})return res
}
力扣 1: 两个数之和
// 时间复杂度O(n) 空间复杂度O(n)functionsum(nums, target){const map =newMap()for(let i =0; i < nums.length; i++){const n = nums[i]const n2 = target - n
if(map.has(n2)){return[map.get(n2), i]}else{
map.set(n, i)}}}
力扣 3:无重复字符的最长子串
3 解题思路
利用双指针(两个变量),找出所有的不含重复字符的子串
找出长度最大那个子串(即右指针减去左指针),返回其长度即可
// 时间复杂度O(n) 空间复杂度O(n)functionlongSubstr(s){let l =0// 左指针let res =0// 计算的最大不重复长度const map =newMap()// 创建Map字典for(let r =0; r < s.length; r ++){// 如果存在字典 并且 当前存在的索引大于等于左指针if(map.has(s[r])&& map.get(s[r])>= l){// 左指针基于当前存在的字典向右移动一位 也就是+1
l = map.get(s[r])+1}// 取res的最大长度
res = Math.max(res, r - l +1)// 设置字典的key及value
map.set(s[r], r)}// 返回最大的长度return res
}