1. 网页手写页面代码具体是怎样写
手写代码的要求是说要求你对HTML、CSS、JS很熟悉,在离开IDE的情况下,用写字板等在没有IDE提示的情况下也能编写代码。
2. 台湾一公司在停电的时候让员工手写代码,你如何看待公司这一要求
台湾一公司在停电的时候让员工手写代码,公司的这个要求真的是有一些无理。手写代码有什么意义呢?来电之后不是还需要重新打在电脑上,那么这个手写就是在浪费时间,对于这样的公司不待也罢,而且感觉公司的老板根本都不懂什么叫做程序,什么叫做代码。
公司方面的回应也是很搞笑,没有办法,不能因为停电影响了工期,在停电之后,公司6名程序员都在工位上面用手机打光用执笔撰写代码,真的好想让这个老板来看一看,或者自己来写一写,写完之后他就知道这个代码到底有没有用了。这个消息也是小编今年以来听到的最搞笑的一个消息,希望大家都能够遇到一个有智商的老板。
3. JavaScript手写LRU算法的示例代码
LRU是Least Recently Used的缩写,即最近最少使用。作为一种经典的缓存策略,它通过将长期未使用的数据替换掉,来提高缓存的命中率。一、基本要求:固定大小,快速访问,以及在达到内存限制的情况下有效驱逐条目。二、数据结构
实现LRU有两种方式:一种是使用Map,另一种是使用双向链表。Map的特点是有序,当我们迭代时,可以按插入顺序返回键值。双向链表则通过增、删、改操作实现数据管理,同时通过Map存储对链表节点的引用以快速读取数据。
三、Map实现
在初始化时,需要配置存储限制和创建Map。在添加数据时,判断是否已存在相同数据并删除,再判断是否已满,若已满则删除头部数据。基于JavaScript Map实现LRU的代码如下:
class LRUCache {
size = 5
constructor(size) {
this.cache = new Map()
this.size = size || this.size
}
get(key) {
if (this.cache.has(key)) {
let temp = this.cache.get(key)
this.cache.delete(key)
this.cache.set(key, temp)
return temp
}
return null
}
set(key, value) {
if (this.cache.has(key)) {
this.cache.delete(key)
}
if (this.cache.size === this.size) {
this.cache.delete(this.cache.keys().next().value)
}
this.cache.set(key, value)
}
四、双向链表实现
1. 定义节点类,包含prev,next,data三个属性。
2. 定义链表类,包含head、tail属性,以及moveNodeToHead,insertNodeToHead,deleteLastNode方法。
3. 定义LRU类,包含linkLine,map和size属性,以及set和get方法。
五、代码实现
class LinkNode {
prev = null
next = null
constructor(key, value) {
this.data = { key, value }
}
}
class LinkLine {
head = null
tail = null
constructor() {
const headNode = new LinkNode('head', 'head')
const tailNode = new LinkNode('tail', 'tail')
headNode.next = tailNode
tailNode.prev = headNode
this.head = headNode
this.tail = tailNode
}
moveNodeToFirst(node) {
node.prev.next = node.next
node.next.prev = node.prev
this.insertNodeToFirst(node)
}
insertNodeToFirst(node) {
const second = this.head.next
this.head.next = node
node.prev = this.head
node.next = second
second.prev = node
}
delete(node) {
node.prev.next = node.next
node.next.prev = node.prev
}
deleteLastNode() {
const last = this.tail.prev
this.tail.prev = last.prev
last.prev.next = this.tail
return last
}
}
class LRUCache {
linkLine = null
map = {}
size = 5
constructor(size) {
this.size = size || this.size
this.linkLine = new LinkLine()
}
get(key) {
let value
if (this.map[key]) {
const node = this.map[key]
value = node.value
this.linkLine.moveNodeToFirst(node)
}
return value
}
set(key, value) {
if (this.map[key]) {
const node = this.map[key]
node.value = value
this.linkLine.moveNodeToFirst(node)
} else {
if (Object.keys(this.map).length === this.size) {
const lastNode = this.linkLine.deleteLastNode()
delete this.map[lastNode.data.key]
}
const newNode = new LinkNode(key, value)
this.linkLine.insertNodeToFirst(newNode)
this.map[key] = newNode
}
}
}