首页
题库
面试
求职
学习
竞赛
More+
所有博客
搜索面经/职位/试题/公司
搜索
我要招人
去企业版
登录 / 注册
首页
>
试题广场
>
连续子链表最大和
[编程题]连续子链表最大和
热度指数:1691
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 256M,其他语言512M
算法知识视频讲解
输入一个单链表,链表中一个或多个连续的整数组成一个子链表。求所有子链表和的最大值。
1.
你能不使用额外的O(n)空间复杂度来完成该题吗?
2.你能使用递归解法来完成该题吗?
3.如果你能使用递归解法完成
本题,本题也可以看成二叉树中的最大路径和的一个子问题,可以尝试递归解法该题之后,去突破二叉树中的最大路径和
数据范围:链表长度满足
,链表中的值满足
示例1
输入
{1,-2,3,10,-4,7,2,-5}
输出
18
示例2
输入
{2}
输出
2
示例3
输入
{-10}
输出
-10
说明:本题目包含复杂数据结构ListNode,
点此查看相关信息
马上挑战
算法知识视频讲解
提交运行
算法知识视频讲解
添加笔记
求解答(0)
邀请回答
收藏(25)
分享
纠错
提交结果有问题?
9个回答
13篇题解
开通博客
太阳hxy
发表于 2023-07-14 13:25:34
连续子链表的最大和 思路: 1.由于需要求最长的连续子串之和,所以先可以初始化为最小值 2.用动态规划的解决连续的最长的子串和的思想 3.用一个个max1记录两种选择中较大值:一种情况是将当前的数加到原先串的后面,另一种情况是将该数直接作为一个新的串的起始点 4.再与之前记录下来的最大的连续子串
展开全文
网络2201林贺文
发表于 2024-03-28 23:34:55
/** * struct ListNode { * int val; * struct ListNode *next; * ListNode(int x) : val(x), next(nullptr) {} * }; */ class Solution { public: /*
展开全文
Sousey
发表于 2022-02-28 09:58:41
动态规划(C++) 这里想法还是比较简单的,和上一题基本无异,只是将数组变成了链表,动态规划比较重要的就是可查询的历史值,减少代码复用。 连续子数组最大和(上一题) https://www.nowcoder.com/practice/1718131e719746e9a56fb29c40cc8f95
展开全文
牛客82035003号
发表于 2022-04-12 21:43:27
和之前求子数组的最大和一样的思路。 把当前链表和nowsum和所求链表和的最大值maxsum均设为第一个元素值, 然后从第二个元素值往后遍历,如果当前和小于0,那么就舍弃前面的求和,把当前值作为第一个元素开始求和, 如果当前和不小于0,那么就把当前值加入成为新的数组和, 再与
展开全文
姐姐的遮阳伞
发表于 2022-04-04 15:21:56
import java.util.*; /* * public class ListNode { * int val; * ListNode next = null; * public ListNode(int val) { * this.val = val; *
展开全文
fred-coder
发表于 2022-02-13 13:38:31
利用动态规划的思想,最大子链表的和只与之前子链表+当前值 和 当前值有关,并用一个全局值记录整个过程中出现的最大值 # class ListNode: # def __init__(self, x): # self.val = x # self.next =
展开全文
牛客494732号
发表于 2023-05-23 11:38:11
package main import . "nc_tools" /* * type ListNode struct{ * Val int * Next *ListNode * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 *
展开全文
牛客276339250号
发表于 2023-11-13 22:25:27
import java.util.*; /* * public class ListNode { * int val; * ListNode next = null; * public ListNode(int val) { * this.val = val; *
展开全文
码主
发表于 2024-04-22 15:13:19
/* * function ListNode(x){ * this.val = x; * this.next = null; * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListN
展开全文
Coming680
发表于 2022-03-01 22:05:14
解法一,动态规划解决 /** * struct ListNode { * int val; * struct ListNode *next; * ListNode(int x) : val(x), next(nullptr) {} * }; */ int num[100000] = {0
展开全文
问题信息
动态规划
记忆化搜索
链表
难度:
9条回答
25收藏
4436浏览
热门推荐
通过挑战的用户
查看代码
企鹅暴打皮卡丘
2022-09-16 15:53:37
牛客1薛之原
2022-09-15 20:23:54
王小牛123
2022-09-14 15:54:08
xzd-698
2022-09-11 14:19:24
用户抉择
2022-09-09 11:54:36
相关试题
圆圈
记忆化搜索
评论
(0)
送分了QAQ
前缀和
记忆化搜索
评论
(0)
乌龟跑步
深度优先搜索(DFS)
记忆化搜索
评论
(0)
模型稀疏化(Pruning)技术主...
大模型概念
评论
(1)
以下哪种方法主要用于缓解大模型训练...
大模型概念
评论
(1)
连续子链表最大和
扫描二维码,关注牛客网
意见反馈
下载牛客APP,随时随地刷题
import java.util.*; /* * public class ListNode { * int val; * ListNode next = null; * public ListNode(int val) { * this.val = val; * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @return int整型 */ public int FindGreatestSumOfSubArray (ListNode head) { // write code here } }
/** * 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 } };
#coding:utf-8 # class ListNode: # def __init__(self, x): # self.val = x # self.next = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param head ListNode类 # @return int整型 # class Solution: def FindGreatestSumOfSubArray(self , head ): # write code here
using System; using System.Collections.Generic; /* public class ListNode { public int val; public ListNode next; public ListNode (int x) { val = x; } } */ class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @return int整型 */ public int FindGreatestSumOfSubArray (ListNode head) { // write code here } }
/* * function ListNode(x){ * this.val = x; * this.next = null; * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @return int整型 */ function FindGreatestSumOfSubArray( head ) { // write code here } module.exports = { FindGreatestSumOfSubArray : FindGreatestSumOfSubArray };
val = $x; } }*/ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @return int整型 */ function FindGreatestSumOfSubArray( $head ) { // write code here }
# class ListNode: # def __init__(self, x): # self.val = x # self.next = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param head ListNode类 # @return int整型 # class Solution: def FindGreatestSumOfSubArray(self , head: ListNode) -> int: # write code here
package main import "fmt" import . "nc_tools" /* * type ListNode struct{ * Val int * Next *ListNode * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @return int整型 */ func FindGreatestSumOfSubArray( head *ListNode ) int { // write code here }
/** * struct ListNode { * int val; * struct ListNode *next; * }; */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @return int整型 */ int FindGreatestSumOfSubArray(struct ListNode* head ) { // write code here }
# class ListNode # attr_accessor :val, :next # # def initialize(val = 0, _next = nil) # @val, @next = val, _next # end # end # # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param head ListNode类 # @return int整型 # class Solution def FindGreatestSumOfSubArray(head) # write code here end end
/** * class ListNode(var val: Int) { * var next: ListNode = null * } */ object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @return int整型 */ def FindGreatestSumOfSubArray(head: ListNode): Int = { // write code here } }
/** * class ListNode(var `val`: Int) { * var next: ListNode? = null * } */ object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @return int整型 */ fun FindGreatestSumOfSubArray(head: ListNode?): Int { // write code here } }
import java.util.*; /* * public class ListNode { * int val; * ListNode next = null; * public ListNode(int val) { * this.val = val; * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @return int整型 */ public int FindGreatestSumOfSubArray (ListNode head) { // write code here } }
/*class ListNode { * val: number * next: ListNode | null * constructor(val?: number, next?: ListNode | null) { * this.val = (val===undefined ? 0 : val) * this.next = (next===undefined ? null : next) * } * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @return int整型 */ export function FindGreatestSumOfSubArray(head: ListNode): number { // write code here }
/** * public class ListNode { * public var val: Int * public var next: ListNode? * public init(_ val: Int = 0, _ next: ListNode? = nil) { * self.val = val * self.next = next * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @return int整型 */ func FindGreatestSumOfSubArray ( _ head: ListNode?) -> Int { // write code here } }
/** * #[derive(PartialEq, Eq, Debug, Clone)] * pub struct ListNode { * pub val: i32, * pub next: Option
> * } * * impl ListNode { * #[inline] * fn new(val: i32) -> Self { * ListNode { * val: val, * next: None, * } * } * } */ struct Solution{ } impl Solution { fn new() -> Self { Solution{} } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @return int整型 */ pub fn FindGreatestSumOfSubArray(&self, head: Option
>) -> i32 { // write code here } }
{1,-2,3,10,-4,7,2,-5}
18
{2}
2
{-10}
-10