和为定值子序列c语言,[蓝桥杯][算法提高VIP]和最大子序列 (C语言代码)时间复杂度 O(n)...

解题思路:

数据的输入就不说了吧;说说核心和思想:贪心加判断

比如这么一串数字        3  -2  3  1  -6  7  1          (再来两个变量   thissum   和    maxsum)

maxsum用来保存当前最大子序列的和,thissum用来保存一定连续范围内的序列和(注意我说的是一定范围内),thissum 和 maxsum初值看我的代码中;当然maxsum你赋值-2100000000是最好的,最后别赋值0;因为有可能最大子序列和就为负数; 好,来看看具体逻辑;用thissum保存一定范围的序列和,最开始thissum加3,自然大于maxsum,然后把3赋值给maxsum,然后thissum加-2为1小于maxsum,继续thissum加3为4大于maxsum,把4赋值给maxsum;继续thissum加1为5大于maxsum,把5赋值maxsum,继续thissum加-6为-1小于maxsum,注意了,这时说明到这块断了,也就是说再继续试验后面的子序列中还有没有比我们当前得出的值大的;因为下次还要用到thissum,所以把thissum置0(thissum小于0的时候),继续thissum加7等于7大于了maxsum,把7赋值给maxsum,(

注意仔细品读这个过程,不懂得留言,会尽快恢复,谢谢

注意事项:

参考代码:#include 

#include 

#include 

#include 

#include 

#include 

using namespace std;

const int maxn = 100001;

const int INF = -2100000000;

int a[maxn];

int maxsum(int * A, int n)

{

int thissum = 0, maxm = INF;

for(int i = 0; i 

{

thissum += A[i];

if(thissum > maxm)

{

maxm = thissum;

}

if(thissum 

{

thissum = 0;

}

}

return maxm;

}

int main()

{

int T;

scanf("%d", &T);

for(int i = 0; i 

{

scanf("%d", &a[i]);

}

printf("%d\n", maxsum(a,T));

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
二叉树基本操作包括:创建二叉树、遍历二叉树、查找节点、插入节点、删除节点等。下面是这些操作的实现代码和时间/空间复杂度分析。 1. 创建二叉树 ``` #include <stdio.h> #include <stdlib.h> struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; }; struct TreeNode* createTree() { int val; scanf("%d", &val); if (val == -1) { // -1表示该节点为空 return NULL; } struct TreeNode *root = (struct TreeNode *)malloc(sizeof(struct TreeNode)); root->val = val; root->left = createTree(); root->right = createTree(); return root; } ``` 时间复杂度:O(n),n为二叉树的节点数,因为要遍历每个节点。 空间复杂度:O(n),即为递归栈的深度,最坏情况下为树的高度,即n。 2. 遍历二叉树 (1)前序遍历 ``` void preorder(struct TreeNode *root) { if (root == NULL) { return; } printf("%d ", root->val); preorder(root->left); preorder(root->right); } ``` 时间复杂度:O(n),n为二叉树的节点数,因为要遍历每个节点。 空间复杂度:O(h),h为二叉树的高度,最坏情况下为n。 (2)中序遍历 ``` void inorder(struct TreeNode *root) { if (root == NULL) { return; } inorder(root->left); printf("%d ", root->val); inorder(root->right); } ``` 时间复杂度:O(n),n为二叉树的节点数,因为要遍历每个节点。 空间复杂度:O(h),h为二叉树的高度,最坏情况下为n。 (3)后序遍历 ``` void postorder(struct TreeNode *root) { if (root == NULL) { return; } postorder(root->left); postorder(root->right); printf("%d ", root->val); } ``` 时间复杂度:O(n),n为二叉树的节点数,因为要遍历每个节点。 空间复杂度:O(h),h为二叉树的高度,最坏情况下为n。 3. 查找节点 ``` struct TreeNode* find(struct TreeNode *root, int val) { if (root == NULL || root->val == val) { return root; } struct TreeNode *left = find(root->left, val); struct TreeNode *right = find(root->right, val); return left == NULL ? right : left; } ``` 时间复杂度:O(n),n为二叉树的节点数,因为要遍历每个节点。 空间复杂度:O(h),h为二叉树的高度,最坏情况下为n。 4. 插入节点 ``` void insert(struct TreeNode *root, int val) { if (root == NULL) { return; } if (root->left == NULL) { root->left = (struct TreeNode *)malloc(sizeof(struct TreeNode)); root->left->val = val; root->left->left = NULL; root->left->right = NULL; return; } if (root->right == NULL) { root->right = (struct TreeNode *)malloc(sizeof(struct TreeNode)); root->right->val = val; root->right->left = NULL; root->right->right = NULL; return; } insert(root->left, val); insert(root->right, val); } ``` 时间复杂度:O(n),n为二叉树的节点数,因为要遍历每个节点。 空间复杂度:O(h),h为二叉树的高度,最坏情况下为n。 5. 删除节点 ``` struct TreeNode* delete(struct TreeNode *root, int val) { if (root == NULL) { return NULL; } if (root->val == val) { if (root->left == NULL && root->right == NULL) { free(root); return NULL; } if (root->left == NULL) { struct TreeNode *right = root->right; free(root); return right; } if (root->right == NULL) { struct TreeNode *left = root->left; free(root); return left; } struct TreeNode *min = root->right; while (min->left != NULL) { min = min->left; } root->val = min->val; root->right = delete(root->right, min->val); } else if (root->val > val) { root->left = delete(root->left, val); } else { root->right = delete(root->right, val); } return root; } ``` 时间复杂度:O(n),n为二叉树的节点数,因为要遍历每个节点。 空间复杂度:O(h),h为二叉树的高度,最坏情况下为n。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值