计算矩形的面积
- 时间复杂度 O(1),空间复杂度O(1)
- Runtime: 189 ms, faster than 25.97%
- Memory Usage: 46.3 MB, less than 16.88%
/**
* @param {number} ax1
* @param {number} ay1
* @param {number} ax2
* @param {number} ay2
* @param {number} bx1
* @param {number} by1
* @param {number} bx2
* @param {number} by2
* @return {number}
*/
var computeArea = function(ax1, ay1, ax2, ay2, bx1, by1, bx2, by2) {
const x = Math.min(ax2, bx2) - Math.max(ax1, bx1);
const y = Math.min(ay2, by2) - Math.max(ay1, by1);
return (ax2 - ax1) * (ay2 - ay1) + (bx2 - bx1) * (by2 - by1) - (Math.max(x, 0)) * (Math.max(y, 0));
};
网友评论