-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
快手面试题(day6) #22
Comments
/**
* 实现 repeat 函数
* @param {Function} 执行函数
* @param {number} 执行次数
* @param {number} 执行间隔
*/
const repeatFunc = repeat(console.log, 4, 1000)
repeatFunc('hello')
/**
* 实现 repeat 函数
* @param {Function} 执行函数
* @param {number} 执行次数
* @param {number} 执行间隔
*/
function repeat(handler, count, wait) {
let remain = count
const repeatFunction = function(...args) {
if(remain === 0) return
const arg = args[0]
if(wait === 0) {
// 立即执行
handler(arg)
remain--;
} else {
// 间隔执行
handler.id = setTimeout(() => {
clearTimeout(handler.id)
handler(arg)
remain--
if(remain>0) {
repeatFunc(arg)
}
}, wait)
}
}
return repeatFunction
}
const repeatFunc = repeat(console.log, 4, 1000)
repeatFunc('hello') |
let arr = [
{a: 1, b: 2},
{b: 2, a: 1},
{a:'1', b:2}
]
/**
* 去重
* 要求: 时间复杂度尽量最低
*/
const newArr = filters(arr)
/**
newArr = [
{a: 1, b: 2},
{a:'1', b:2}
]
*/
let arr = [
{a: 1, b: 2},
{b: 2, a: 1},
{a:'1', b:2}
]
function filters(arr) {
const list = arr.slice()
let set = new Set()
for(let item of list) {
set.add(JSON.stringify({
a: item['a'],
b: item['b']
}))
}
return [...set].map(item => JSON.parse(item));
}
/**
* 去重
* 要求: 时间复杂度尽量最低
*/
const newArr = filters(arr)
console.log(newArr)
/**
newArr = [
{a: 1, b: 2},
{a:'1', b:2}
]
*/ |
// 实现一个 EventEmit 类
class EventEmit {
on() {}
once() {}
off() {}
emit() {}
}
// 实现一个 EventEmit 类
class EventEmit {
constructor() {
this.eventMap = {}
}
on(name, handler) {
if(Array.isArray(this.eventMap[name])) this.eventMap[name] = []
if(typeof handler === 'function') {
this.eventMap[name].push(handler)
}
}
once(name, handler) {
if(Array.isArray(this.eventMap[name])) this.eventMap[name] = []
handler.once = true
if(typeof handler === 'function') {
this.eventMap[name].push(handler)
}
}
off(name, handler) {
if(Array.isArray(this.eventMap[name])) this.eventMap[name] = []
const queue = this.eventMap[name];
const index = queue.findIndex(item => item === handler);
queue.splice(index, 1);
this.eventMap[name] = queue
}
emit(name, ...args) {
if(Array.isArray(this.eventMap[name])) this.eventMap[name] = []
const queue = this.eventMap[name];
for(let handler of queue) {
if(typeof handler === 'function') {
handler(args)
}
if(handler.once) {
handler = null
}
}
this.eventMap[name] = queue.filter(item => item)
}
} |
// linkNode
// [1,2,3]
// [2,4,5]
// [1,2,2,3,4,5]
/**
* LinkNode
* @param {number | null} value
* @param {LinkNode | null} next
*/
/**
*
* @param {LinkNode} link1
* @param {LinkNode} link2
*/
function merge(link1, link2) {
let point = new LinkNode(0)
let link = point
// 遍历两个链表
while(link1 || link2) {
if(link1 && link2) {
if(link1.value < link2.value) {
point.next = link1
link1 = link1.next
} else {
point.next = link2
link2 = link2.next
}
point = point.next
} else if(link1) {
// link2 === null
point.next = link1
point = point.next
link1 = link1.next
} else {
// link1 === null
point.next = link2
point = point.next
link2 = link2.next
}
}
return link.next
}
merge([], []) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Q1:
Q2:
Q3:
Q4:
Q5:
Q6:
The text was updated successfully, but these errors were encountered: