数组类算法解题思想
当要解决一个求一个数组里面具有某种性质的一组元素,可以加个限制条件,比如以第i个结尾的子序列的这种性质,求出n个结果,而
最终答案一定落在这n个数字里面,遍历一遍就可以了。
示例:
Given an integer array nums
, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
Example:
Input: [-2,1,-3,4,-1,2,1,-5,4], Output: 6 Explanation: [4,-1,2,1] has the largest sum = 6.