funcmaxSubArray2(_nums: [Int]) -> [Int] { var pre =0 var maxValue = nums[0] var start =0// 最大连续子数组的起始位置 var end =0// 最大连续子数组的结束位置 for (idx, val) in nums.enumerated() { if pre <0 { pre = val if pre > maxValue { maxValue = pre start = idx end = idx } } else { pre = pre + val if pre > maxValue { maxValue = pre end = idx } } } returnArray(nums[start...end]) }