class Solution {
public int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {
// 找到overlap(如果有)的左下和右上2个坐标8个点
int leftTopX = Math.max(ax1, bx1);
int leftTopY = Math.min(ay2, by2);
int leftBotX = leftTopX;
int leftBotY = Math.max(ay1, by1);
int rightTopX = Math.min(ax2, bx2);
int rightTopY = leftTopY;
int rightBotX = rightTopX;
int rightBotY = leftBotY;
// 2个长方形之和
int total = Math.abs(ax2 - ax1) * Math.abs(ay2 - ay1) + Math.abs(bx2 - bx1) * Math.abs(by2 - by1);
//判断2个长方形如果没有重叠
if(ax2 <= bx1 || ax1 >= bx2 || ay1 >= by2 || by1 >= ay2) {
return total;
}
// 不然就是有重叠 减去重叠部分
return total - Math.abs(rightTopX - leftTopX) * Math.abs(rightTopY - rightBotY);
}
}
172. Factorial Trailing Zeroes
class Solution {
public int trailingZeroes(int n) {
// 10 = 2 * 5 统计2 和 5 的个数,偶数都有2, 所以统计5的个数
/*
O(log_5(N)) (base 5!) is faster than any polynomial.
You need no more than 14 iterations to get the result.
You just need to count how many times 5 appears in n!
during multiplication in different forms: 5, 25, 125, 625, ... .
when any 5 is multiplied by 2 (we have many of them) then we get 0 at the end.
*/
int res = 0;
while(n > 0) {
n = n / 5;
res += n;
}
return res;
}
}