C数据结构-数组前缀和

走远了吗. 提交于 2020-03-10 03:56:52

leetcode289题,生命游戏。
思路:把数组边界扩充。
leetcode1109题,航班预订统计。
思路:先存前缀,最后去求和。

参考如下作者的算法: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;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!