41 lines
681 B
Go
41 lines
681 B
Go
package main
|
|
|
|
import "fmt"
|
|
|
|
func main() {
|
|
fmt.Println(maxSubArray([]int{-1, 0, -2}))
|
|
}
|
|
|
|
func maxSubArray(nums []int) int {
|
|
if len(nums) == 1 {
|
|
return nums[0]
|
|
}
|
|
result := nums[0]
|
|
value := 0
|
|
tmp := make([]int, 0)
|
|
for i := range nums {
|
|
value += nums[i]
|
|
tmp = append(tmp, value)
|
|
}
|
|
fmt.Println(tmp)
|
|
minnum := make([]int, len(tmp)+1)
|
|
minnum[0] = 0
|
|
for i, v := range tmp {
|
|
//fmt.Println("当前数字:", nums[i])
|
|
minnum[i+1] = min(v, minnum[i])
|
|
temp := tmp[i] - minnum[i]
|
|
fmt.Println("当前temp", temp)
|
|
|
|
if result <= temp {
|
|
result = temp
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Println("minnum", minnum)
|
|
////fmt.Println("maxnum", maxnum)
|
|
//fmt.Println(result)
|
|
|
|
return result
|
|
}
|