swift O(n) Solution


  • 0
    L
    class Solution {
        func maxSubArray(_ nums: [Int]) -> Int {
            if nums.count == 1{
                return nums.first!
            }
            var total = 0; var max = nums[0]
            for i in 0..<nums.count {
                total += nums[i]
                if total > max {
                    max = total
                } 
                if total < 0 {
                    total = 0;
                }
            }
            return max
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.