愛放△進行李々 2022-05-04 13:56:26
[...new Set([...Array(10)].map(x = >parseInt(Math.random() * 10)))].sort((x, y) = >x - y).reduce(function(acc, cur, index, source) {
if (cur - acc[acc.length - 1] !== 1 && acc.length > 0) {
acc.push('-')
}
acc.push(cur) return acc
},
[]).toString().replace(/,/g, '').split('-').map((x) = >[x.split('')])
- 共 1 页
- 1
function intersect(m,n){
return new Set(m).size<=new Set(n).size?n.filter(x=>new Set(m).has(x)):m.filter(x=>new Set(n).has(x))
}
console.log(intersect([1, 2, 2, 1],[2, 2, 1]))
console.log(intersect([1,1],[1,2]))
第 59 题:给定两个数组,写一个方法来计算它们的交集