动态规划(C++)
这里想法还是比较简单的,和上一题基本无异,只是将数组变成了链表,动态规划比较重要的就是可查询的历史值,减少代码复用。
连续子数组最大和(上一题)
https://www.nowcoder.com/practice/1718131e719746e9a56fb29c40cc8f95
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) : val(x), next(nullptr) {}
* };
*/
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类
* @return int整型
*/
int FindGreatestSumOfSubArray(ListNode* head) {
// write code here
int max_num = INT_MIN;
int max_n = INT_MIN;
while(head != nullptr){
max_num = max(head->val + max_num , head->val);
max_n = max(max_n ,max_num);
head = head->next;
//cout << max_num <<endl;
}
return max_n;
}
};