笔试题(实例)

一些常见笔试应用题

用户喜好(字节跳动)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// 输入
let userFav = [1, 2, 3, 3, 5]
let queryForm = [{
users: [1, 2],
stars: 1
}, {
users: [2, 4],
stars: 5
}, {
users: [3, 5],
stars: 3
}]

//输出
let queryFormResult = [] //[1, 0, 2]

function fun() {
queryForm.map(e => {
let f = 0
let usersStars = []
for (let i = e.users[0] - 1; i < e.users[1]; i++) {
if (userFav[i] === e.stars) f = f + 1
}
queryFormResult.push(f)
})
}

// 执行
fun()
console.log(queryFormResult)