go语言刷leetcode - 53 Maximum Subarray

package main

import (
    "fmt"
    "math"
)

func maxSubArray(nums []int) int {
    var largestSum float64 = -math.MaxFloat64
    var currentSum float64 = -math.MaxFloat64
    for i := 0; i < len(nums); i++ {
        currentSum = math.Max(currentSum+float64(nums[i]), float64(nums[i]))
        largestSum = math.Max(largestSum, currentSum)
    }
    return int(largestSum)
}

func main() {
    fmt.Println(maxSubArray([]int{-1}))
}

 

posted on 2018-10-09 15:31  于敬晖  阅读(88)  评论(0编辑  收藏  举报

导航