const instanceof = (target,obj) => { let p = target while(p){ if(p == obj.prototype){ return true } p = p.__proto__ } return false } console.log(instanceof([1,2,3],Array))
环形链表
leetcode-141环形链表
1 2 3 4 5 6 7 8 9 10
function hasCycle = function(head){ let f = head let s = head while( f! = null && f.next != null){ s = s.next f = f.next.next if( s = f ) return true } return false }
leetcode-237删除链表中的节点
1 2 3 4 5
无法访问head,只能访问要删除的node,要删除的node不是末尾节点 function deleteEle(node){ node.val = node.next.val node.next = node.next.next }
给一个head,升序序列,删除所有重塑元,使每个元素只出现一次 function deleteEle(head){ //我写的 let p = head while( p.val = p.next.val ){ p.next.val = p.next.next.val p.next.next = p.next.next.next } //视频写的 if(!head){ return head } let cur = head while(cur.next){ if(cur.val = cur.next.val){ cur.next = cur.next.next }else{ cur = cur.next } } return head }
leetcode-206反转链表
1 2 3 4 5 6 7 8 9 10 11
function reserve(head){ let prev = null let curr = head while(curr){ const next = curr.next curr.next = prev prev = curr curr = next } return prev }
字典和哈希表
字典
键值对的存储,类似js对象,js对象的键在某些情况会转换成对象类型,字典类型键不会转换类型
1 2 3 4 5 6 7
let map = new Map() map.set('key',123) map.get('key') map.has('key') map.delete('key') map.clear() console.log(map.size) //length
实现哈希表 class Hash { constructor{ this.table = [] } hashCode(key){ let hash = 0 for(let i = 0; i < key.length; i++){ hash += key.charCodeAt(i) } return hash } put(key,value){ let hashKey = this.hashCode(key) this.table[hashKey] = value } get(key){ let hashKey = this.hashCode(key) return this.table[hashKey] } } let hash = new Hash()
leetcode哈希表1两数之和
1 2 3 4 5 6 7 8 9 10 11
给定一个数组,给定一个目标值,求数组中和为目标值的两个整数,返回下标 function computed(nums, target){ let map = new Map() for(let i = 0; i < nums.length; i++){ num = target - nums[i] if(map.has(num)){ return [map.get(num),i] } map.set(nums[i],i) } }
leetcode哈希表存在重复元素
1 2 3 4 5 6 7 8 9 10 11 12
给定一个数组,判断是否存在重复元素 如果一个值在该数组中至少出现两次,函数返回true,如果数组中每个元素都不相同,返回false function same(nums){ let map = new Map() for(let i = 0; i < nums.length;i++){ if(map.has(nums[i])){ return true } map.set(nums[i],i) } return false }
leetcode哈希表349两个数组交集
1 2 3 4 5
给定两个数组,编写函数计算交集 function com(nums1,nums2){ let set = new Set(nums) return [...new Set(nums1)].filter(val => set.has(val)) }
判断一个字符串中出现最多的字符并统计次数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
function num(str){ let maxNum = 0 let maxStr = '' let map = new Map() for(let item of str){ map.set(item, (map.get(item) || 0) + 1) } for(let [key,value] of map){ if(value > maxNum){ maxNum = value maxStr = key } } return [maxStr,maxNum] }
leetcode哈希表1207独一无二的出现次数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
给予一个都是整数项的数组,统计每个项出现的次数,如果每个数出现的次数都是独一无二的,返回true,否则返回false function U(arr){ const map = new Map() for(let item of arr){ if(map.has(item)){ map.set(item,map.get(item) + 1) }else{ map.set(item,1) } } const set = new Set() for(let [key,value] of map){ set.add(value) } return set.size == map.size }
function tree(root){ if(!root)return 0 const stack = [root] let num = 0 while(stack.length){ let len = stack.length num++ while(len--){ const o = stack.shift() o.left && stack.push(o.left) o.right && stack.push(o.right) } } return num }
leetcode104翻转二叉树
1 2 3 4 5 6 7 8 9
function tree(root){ if(root == null)return null let tem = root.left root.left = rot.right root.right = tem tree(root.left) tree(root.right) return root }