输入一个单链表,链表中一个或多个连续的整数组成一个子链表。求所有子链表和的最大值。 1.你能不使用额外的O(n)空间复杂度来完成该题吗? 2.你能使用递归解法来完成该题吗? 3.如果你能使用递归解法完成本题,本题也可以看成二叉树中的最大路径和的一个子问题,可以尝试递归解法该题之后,去突破二叉树中的最大路径和 数据范围:链表长度满足 ,链表中的值满足
示例1
输入
{1,-2,3,10,-4,7,2,-5}
输出
18
示例2
输入
{2}
输出
2
示例3
输入
{-10}
输出
-10
加载中...
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