560. 和为K的子数组 Subarray Sum Equals K

题目 <https://leetcode-cn.com/problems/subarray-sum-equals-k/>  

常规超时

int subarraySum(int* nums, int numsSize, int k){
    int *sum = malloc(sizeof(int) * (numsSize+1));
    sum[0] = 0;
    int i,j,count=0;
    for(i=0;i<numsSize;i++){
        sum[i+1] = nums[i] + sum[i];
    }

    for(i=1;i<=numsSize;i++){
        for(j=0;j<i;j++){
            if(sum[i] - sum[j] == k){
                count++;
            }
        }
    }
    return count;
}

红黑树吧...

struct Node{
	int key;
	int count;
	struct Node *left,*right;
	int isRED;
};

struct Node* getValue(struct Node *root,int key){
	if(root == NULL){
		return NULL;
	}
	if(root->key == key){
		return root;
	}
	else if(root->key < key){
		return getValue(root->right,key);
	}else{
		return getValue(root->left,key);
	}
}

struct Node* balance(struct Node* root){
	if(root->left != NULL && root->right != NULL && root->left->isRED == 1 && root->right->isRED == 1)
	{
		root->left->isRED = 0;
		root->right->isRED = 0;
		root->isRED = 1;
		return root;
	}

	if(root->left != NULL && root->left->isRED == 1){
		if(root->left->left!=NULL && root->left->left->isRED == 1){
			struct Node *node = root;
			root = node->left;
			struct Node* right = root->right;

			root->left->isRED = 0;
			root->right = node;
			root->right->isRED = 0;
		
			node->left = right;
			return root;
		}
		if(root->left->right!=NULL && root->left->right->isRED == 1){
			struct Node *node = root;
			root = node->left->right;
			struct Node* left = root->left;
			struct Node* right = root->right;

			root->left = node->left;
			root->left->right = left;
			root->left->isRED = 0;
			root->right = node;

			node->left = right;
			return root;
		}
	}
	if(root->right != NULL && root->right->isRED == 1){
		if(root->right->left!=NULL && root->right->left->isRED == 1){
			struct Node *node = root;
			root = root->right->left;
			struct Node* left = root->left;
			struct Node* right = root->right;

			root->left = node;
			root->right = node->right;
			root->right->left=right;
			root->right->isRED = 0;

			node->right=left;
			return root;
		}
		if(root->right->right!=NULL && root->right->right->isRED == 1){
			struct Node *node = root;
			root = node->right;
			struct Node* left = root->left;

			root->left = node;
			root->left->isRED = 0;
			root->right->isRED = 0;

			node->right = left;
			return root;
		}
	}
	return root;
}

struct Node* putValue(struct Node *root,int key){
	if(root == NULL){
		struct Node* node = malloc(sizeof(struct Node));
		memset(node,0,sizeof(node));
		node->key = key;
		node->count = 1;
		node->isRED = 1;
		node->left = NULL;
		node->right = NULL;
		return node;
	}

	if(root->key == key){
		root->count++;
		return root;
	}
	else if(root->key < key){
		root->right = putValue(root->right,key);
	}else{
		root->left = putValue(root->left,key);
	}
	root = balance(root);
	return root;
}

void freeTree(struct Node* root){
	if(root == NULL)
		return;
	freeTree(root->left);
	freeTree(root->right);
	free(root);
}

int subarraySum(int* nums, int numsSize, int k){
    int i,j,sum = 0,count=0;
    struct Node *root = NULL,*node=NULL;
    sum = 0;
    root = putValue(root,sum);
    for(i=0;i<numsSize;i++){
        sum += nums[i];
        j = sum - k;
        node = getValue(root,j);
        if(node != NULL){
            count += node->count;
        }
        root = putValue(root,sum);
    }
    freeTree(root);
    return count;
}

猜你喜欢

转载自blog.csdn.net/ZRXSLYG/article/details/111640013
今日推荐