leetcode289题,生命游戏。
思路:把数组边界扩充。
leetcode1109题,航班预订统计。
思路:先存前后两个变化数组的点,差分的思路,最后去求前缀和。
leetcode370题,求区间加法。
思路:先存前后两个变化数组的点,差分的思路,最后去求前缀和。
leetcode304题,二维区域和检索 - 矩阵不可变。
思路,扩充数组上左边界,存区域和。注意:要检查二维数组是否为空。
参考如下作者的算法:https://leetcode-cn.com/problems/corporate-flight-bookings/solution/qian-zhui-he-fa-python-java-shi-jian-fu-za-du-on-b/
int* corpFlightBookings(int** bookings, int bookingsSize, int* bookingsColSize, int n, int* returnSize){
*returnSize = n;
int * ret = (int *)malloc(sizeof(int) * n);
int i, j;
memset(ret, 0, sizeof(int) * n);
for (i = 0; i < bookingsSize; i++){
ret[bookings[i][0] -1] += bookings[i][2];
if (bookings[i][1] < n) {
ret[bookings[i][1]] -= bookings[i][2];
}
}
for (j = 1; j < n;j++) {
ret[j] += ret[j-1];
}
return ret;
}
int* getModifiedArray(int length, int** updates, int updatesSize, int* updatesColSize, int* returnSize){
* returnSize = length;
if (length == 0) {
return NULL;
}
int tmp[80002] = {
0};
int i;
int *ret = (int*)malloc(sizeof(int)*length);
for (i = 0; i < updatesSize; i++) {
tmp[updates[i][0]] += updates[i][2];
tmp[updates[i][1] + 1] += 0 - updates[i][2];
}
for (i = 0; i < length; i++) {
ret[i] = i == 0 ? tmp[i] : tmp[i] + ret[i-1];
}
return ret;
}