当要解决一个求一个数组里面具备某种性质的一组元素,能够加个限制条件,好比以第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.