题目 <https://leetcode-cn.com/problems/range-sum-query-mutable/>
普通做法
typedef struct {
int *nums;
int numsSize;
int *sum;
int *update;
} NumArray;
NumArray* numArrayCreate(int* nums, int numsSize) {
NumArray *obj = malloc(sizeof(NumArray));
obj->nums = NULL;
obj->numsSize = 0;
obj->sum = NULL;
obj->numsSize = numsSize;
obj->nums = malloc(sizeof(int) * numsSize);
memcpy(obj->nums,nums,sizeof(int) * numsSize);
obj->sum = malloc(sizeof(int) * (numsSize+1));
obj->sum[0] = 0;
int i;
for(i=0;i<numsSize;i++){
obj->sum[i+1] = nums[i] + obj->sum[i];
}
obj->update = malloc(sizeof(int) * numsSize);
memset(obj->update,0,sizeof(int) * numsSize);
return obj;
}
void numArrayUpdate(NumArray* obj, int i, int val) {
obj->update[i] = val - obj->nums[i];
}
int numArraySumRange(NumArray* obj, int i, int j) {
int sum = obj->sum[j+1]-obj->sum[i];
int k;
for(k=i;k<=j;k++){
sum += obj->update[k];
}
//return obj->sum[j+1]-obj->sum[i];
return sum;
}
void numArrayFree(NumArray* obj) {
free(obj->nums);
free(obj->sum);
free(obj->update);
free(obj);
}
线段树,观看并学习了山景城一姐的视频
struct Node{
int val;
int start,end;
struct Node *left,*right;
};
struct Node *buildTree(int* nums, int start,int end){
if(start>end){
return NULL;
}else if(start == end){
struct Node *root = malloc(sizeof(struct Node));
root->start = start;
root->end = end;
root->val = nums[start];
root->left = NULL;
root->right = NULL;
return root;
}else{
struct Node *root = malloc(sizeof(struct Node));
root->start = start;
root->end = end;
int mid = start + (end-start)/2;
root->left = buildTree(nums,start,mid);
root->right = buildTree(nums,mid+1,end);
root->val = root->left->val + root->right->val;
return root;
}
}
int searchTree(struct Node *root,int start,int end){
//if(root == NULL){
// return 0;
//}
if(root->start == start && root->end==end){
return root->val;
}
int mid = root->start + (root->end-root->start)/2;
if(start >= mid+1){
return searchTree(root->right,start,end);
}else if(end<=mid){
return searchTree(root->left,start,end);
}else{
return searchTree(root->left,start,mid) + searchTree(root->right,mid+1,end);
}
}
void updateTree(struct Node *root,int start,int val){
//if(root == NULL){
// return 0;
//}
if(root->start == root->end){
root->val = val;
return;
}
int mid = root->start + (root->end - root->start)/2;
if(start >= mid+1)
{
updateTree(root->right,start,val);
}
else //if(i < mid)
{
updateTree(root->left,start,val);
}
root->val = root->left->val + root->right->val;
}
void freeTree(struct Node *root){
if(root == NULL){
return;
}
freeTree(root->left);
freeTree(root->right);
free(root);
}
typedef struct {
struct Node *root;
} NumArray;
NumArray* numArrayCreate(int* nums, int numsSize) {
NumArray *obj = malloc(sizeof(NumArray));
obj->root = buildTree(nums,0,numsSize-1);
return obj;
}
void numArrayUpdate(NumArray* obj, int i, int val) {
updateTree(obj->root,i,val);
}
int numArraySumRange(NumArray* obj, int i, int j) {
return searchTree(obj->root,i,j);
}
void numArrayFree(NumArray* obj) {
freeTree(obj->root);
free(obj);
}