剑指offer
连续子数组的最大和
相似的企业真题
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 64M,其他语言128M
热度指数:483294
本题知识点:
分治
动态规划
算法知识视频讲解
校招时部分企业笔试将禁止编程题跳出页面,为提前适应,练习时请使用在线自测,而非本地IDE。
题目描述
输入一个整型数组,数组里有正数也有负数。数组中的一个或连续多个整数组成一个子数组。求所有子数组的和的最大值。要求时间复杂度为 O(n).
示例1
输入
复制
[1,-2,3,10,-4,7,2,-5]
[1,-2,3,10,-4,7,2,-5]
返回值
复制
18
18
说明
输入的数组为{1,-2,3,10,—4,7,2,一5},和最大的子数组为{3,10,一4,7,2},因此输出为该子数组的和 18。
上一题
下一题
登录
/
注册
我的提交
编辑器加载中...
public class Solution { public int FindGreatestSumOfSubArray(int[] array) { } }
class Solution { public: int FindGreatestSumOfSubArray(vector
array) { } };
# -*- coding:utf-8 -*- class Solution: def FindGreatestSumOfSubArray(self, array): # write code here
using System; using System.Collections.Generic; class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param array int整型一维数组 * @return int整型 */ public int FindGreatestSumOfSubArray (List
array) { // write code here } }
function FindGreatestSumOfSubArray(array) { // write code here } module.exports = { FindGreatestSumOfSubArray : FindGreatestSumOfSubArray };
function FindGreatestSumOfSubArray(array) { // write code here }
# -*- coding:utf-8 -*- class Solution: def FindGreatestSumOfSubArray(self, array): # write code here
package main /** * * @param array int整型一维数组 * @return int整型 */ func FindGreatestSumOfSubArray( array []int ) int { // write code here }
/** * * @param array int整型一维数组 * @param arrayLen int array数组长度 * @return int整型 */ int FindGreatestSumOfSubArray(int* array, int arrayLen ) { // write code here }
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # @param array int整型一维数组 # @return int整型 # class Solution def FindGreatestSumOfSubArray(array) # write code here end end
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param array int整型一维数组 * @return int整型 */ def FindGreatestSumOfSubArray(array: Array[Int]): Int = { // write code here } }
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param array int整型一维数组 * @return int整型 */ fun FindGreatestSumOfSubArray(array: IntArray): Int { // write code here } }
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param array int整型一维数组 * @return int整型 */ public int FindGreatestSumOfSubArray (int[] array) { // write code here } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param array int整型一维数组 * @return int整型 */ export function FindGreatestSumOfSubArray(array: number[]): number { // write code here }
public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param array int整型一维数组 * @return int整型 */ func FindGreatestSumOfSubArray ( _ array: [Int]) -> Int { // write code here } }
struct Solution{ } impl Solution { fn new() -> Self { Solution{} } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param array int整型一维数组 * @return int整型 */ pub fn FindGreatestSumOfSubArray(&self, array: Vec
) -> i32 { // write code here } }
[1,-2,3,10,-4,7,2,-5]
18