
3. 无重复字符的最长子串建立一个哈希表遍历每个字符将字符下标存进表里left代表不重复子字符串的开始节点right代表遍历索引function lengthOfLongestSubstring(s: string): number { const lastIndex new Mapstring,number() let left 0 let max 0 for(let right0;rights.length;right){ const cur s[right] if(lastIndex.has(cur)){ left Math.max(left,lastIndex.get(cur)1) } lastIndex.set(cur,right) max Math.max(max,right-left1) } return max };146.LRU缓存map是能记录插入顺序的键值对.size能获取哈希表的长度.set(key,value)相同的key新的值覆盖先前的value.has(key)判断key在不在哈希表里.get(key)获取哈希表中key对应的value值.keys()获取哈希表的所有键值.keys().next().value获取哈希表中键的最先插进去的键值class LRUCache { //定义变量 private contain:Mapnumber,number private capacity:number constructor(capacity: number) { this.contain new Map() this.capacity capacity } get(key: number): number { //如果存在删除原来的值重新插入 if(this.contain.has(key)){ const value this.contain.get(key)! this.contain.delete(key) this.contain.set(key,value) return value }else{ return -1 } } put(key: number, value: number): void { if(this.contain.has(key)){ this.contain.delete(key) } this.contain.set(key,value) if(this.contain.sizethis.capacity){ //找到最先插入的键删除 const trail this.contain.keys().next().value this.contain.delete(trail) } } } /** * Your LRUCache object will be instantiated and called as such: * var obj new LRUCache(capacity) * var param_1 obj.get(key) * obj.put(key,value) */206.反转链表1-2-3-4-5-null结果null-1-2-3-4-5pre curcur.next pre cur.next指向nullpre和cur各进一步/** * Definition for singly-linked list. * class ListNode { * val: number * next: ListNode | null * constructor(val?: number, next?: ListNode | null) { * this.val (valundefined ? 0 : val) * this.next (nextundefined ? null : next) * } * } */ function reverseList(head: ListNode | null): ListNode | null { if(!head || head.nextnull) return head let pre:ListNode | null null let cur:ListNode | null head while(cur){ let next cur.next cur.next pre pre cur cur next } return pre };25.K个一组翻转链表先翻转前k个元素递归翻转剩余的链表head表示旧链表的头pre表示新链表的头cur表示下一组翻转链表的开始节点/** * Definition for singly-linked list. * class ListNode { * val: number * next: ListNode | null * constructor(val?: number, next?: ListNode | null) { * this.val (valundefined ? 0 : val) * this.next (nextundefined ? null : next) * } * } */ function reverseKGroup(head: ListNode | null, k: number): ListNode | null { if(!head || k1) return head let count 0 let index:ListNode | null head while(index countk){ index index.next count } if(countk) return head let pre:ListNode | null null let cur:ListNode | null head for(let i0;ik;i){ const next cur.next cur.next pre pre cur cur next } head.next reverseKGroup(cur,k) return pre };15.三数之和滑动窗口1.从小到大排序2.如果num[i]0证明和0不符直接结束循环。有相同的跳过进入下一次循环2.i从0开始最后的索引是倒数第三个ji1;z从最后一个索引开始往前3.计算当前的和0窗口向左移z--0窗口右移j4.相等符合。判断下一个是不是相同的值相同跳过最后z--,jfunction threeSum(nums: number[]): number[][] { nums.sort((a,b)a-b) const res:number[][] [] for(let i0;inums.length-2;i){ if(nums[i]0) break if(i0 nums[i]nums[i-1]) continue let j i1 let k nums.length-1 while(jk){ const sum nums[i]nums[j]nums[k] if(sum0){ k-- }else if(sum0){ j }else{ res.push([nums[i],nums[j],nums[k]]) while(jk nums[j]nums[j1]) j while(jk nums[k]nums[k-1]) k-- j k-- } } } return res };共勉