當要解決一個求一個數組裏面具備某種性質的一組元素,能夠加個限制條件,好比以第i個結尾的子序列的這種性質,求出n個結果,而數組
最終答案必定落在這n個數字裏面,遍歷一遍就能夠了。 app
示例:spa
Given an integer array nums
, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.code
Example:it
Input: [-2,1,-3,4,-1,2,1,-5,4], Output: 6 Explanation: [4,-1,2,1] has the largest sum = 6.