阅读 53

LeetCode第23天🐱‍🏍

447. 回旋镖的数量

n个距离点m相等的点,可得到n*(n-1)个回旋镖.用哈希图存储key距离和value出现次数.

var numberOfBoomerangs = function(points) {
    let sum = 0
    for (let point1 of points) {
        const crl = new Map()
        for (let point2 of points) {
            const dis = 
            Math.sqrt((point1[1]-point2[1]) ** 2 + (point1[0]-point2[0]) ** 2)
            if (point1 != point2) crl.set(dis, (crl.get(dis) || 0) + 1)
        }
        for (const v of crl.values()) {
        sum += v * (v - 1)
    }
    }
    return sum
};
复制代码

70. 爬楼梯

var climbStairs = function(n) {
    if (n == 1 || n == 2) return n
    let p = 1, q = 2, res
    for (let i = 3; i <= n; i++) {
        res = p + q
        p = q
        q = res
    }
    return res
};
复制代码
文章分类
前端