zoukankan      html  css  js  c++  java
  • 找到数组或整数列表中连续子序列的最大和

    题目描述:

    The maximum sum subarray problem consists in finding the maximum sum of a contiguous subsequence in an array or list of integers:

    maxSequence([-2, 1, -3, 4, -1, 2, 1, -5, 4])
    # should be 6: [4, -1, 2, 1]

    Easy case is when the list is made up of only positive numbers and the maximum sum is the sum of the whole array. If the list is made up of only negative numbers, return 0 instead.

    Empty list is considered to have zero greatest sum. Note that the empty list or array is also a valid sublist/subarray.

    我的解答:

    def maximum_subarray_sum(n):
    if len(n) == 0:
    return 0
    max_sum = 0
    max_tmp = 0
    for i in range(len(n)):
    for j in range(i + 1, len(n)):
    pass
    if max_tmp <= 0:
    max_tmp = n[i]
    else:
    max_tmp += n[i]

    if max_sum < max_tmp:
    max_sum = max_tmp
    return max_sum
  • 相关阅读:
    表值函数,标量值函数
    考勤率
    精确小数点
    SQL 返回刚插入的值
    xml读取
    备份表
    case,cast
    DDR基础知识
    NXP官方的I.MX6UL板级Uboot源码适配
    高通msm8909打开debug串口
  • 原文地址:https://www.cnblogs.com/wlj-axia/p/12704997.html
Copyright © 2011-2022 走看看