快慢指针法

function intervalIntersection(A, B) {
    if (A == null || A.length == 0 || B == null || B.length == 0) {
        return []
    }

    let ret = [], i = 0, j = 0, startMax, endMin;
    while (i < A.length && j < B.length) {
        startMax = Math.max(A[i][0], B[j][0]);
        endMin = Math.min(A[i][1], B[j][1]);

        if (endMin >= startMax) {
            ret.push([startMax, endMin]);
        }
        if (A[i][1] == endMin)
            i++;
        if (B[j][1] == endMin)
            j++;
    }
    return ret
}

var A = [[0, 2], [5, 10], [13, 23], [24, 25]],
    B = [[1, 5], [8, 12], [15, 24], [25, 26]]
intervalIntersection(A, B)

相关文章:

  • 2021-09-08
  • 2021-11-23
  • 2022-01-23
  • 2021-07-16
  • 2021-11-25
  • 2022-01-09
猜你喜欢
  • 2021-10-31
  • 2022-12-23
  • 2022-12-23
  • 2021-06-08
  • 2021-06-09
  • 2021-06-22
  • 2022-12-23
相关资源
相似解决方案