class Solution {
public:
int maxSubArray(vector<int>& nums) {
if (nums.empty())
return 0;
int maxNow = nums[0];
int globalMax = maxNow;
for (int i = 1; i < nums.size(); i++){
maxNow = (maxNow + nums[i]) > nums[i] ? (maxNow + nums[i]) : nums[i];
if (maxNow > globalMax)
globalMax = maxNow;
}
return globalMax;
}
};
固定一个点来找:所有的 subarray 转化成结尾为第 i 个元素的 subarray.
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.