leetcode 53. Maximum Subarray
2017-04-06 14:44
483 查看
53. Maximum Subarray c语言实现
DescriptionFind the contiguous subarray within an array (containing at least one number) which has the largest sum.
For example, given the array
[-2,1,-3,4,-1,2,1,-5,4],
the contiguous subarray
[4,-1,2,1]has the largest sum =
6.
int maxSubArray(int* nums, int numsSize) {
int i,j;
int max,temp;
if(numsSize == 0) return 0;
if(numsSize == 1) return nums[0];
max = nums[0];
for(i = 0; i < numsSize - 1; i++)
{
j=i + 1;
if(nums[i] > max) max = nums[i];//单个值比max大
temp = nums[i];
while(j < numsSize)
{
temp += nums[j];
if(temp > max){
max = temp;
}
j++;
}
}
if(nums[i] > max) max = nums[i];//比较最后一个元素
return max;
}
相关文章推荐
- LeetCode53. Maximum Subarray
- LeetCode53. Maximum Subarray 动态规划和分治法
- leetcode: 53. Maximum Subarray
- leetcode 53. Maximum Subarray
- Leetcode 53. Maximum Subarray
- Leetcode 53. Maximum Subarray
- leetcode--3,Longest Substring Without Repeating Characters &&53. Maximum Subarray&&String.valueOf()
- LeetCode 53. Maximum Subarray
- [LeetCode] 53. Maximum Subarray
- 个人记录-LeetCode 53. Maximum Subarray
- Leetcode 53. Maximum Subarray
- 【一天一道LeetCode】#53. Maximum Subarray
- LeetCode 53. Maximum Subarray
- [leetcode] 53. Maximum Subarray 解题报告
- 第二周:[LeetCode]53. Maximum Subarray
- 【LeetCode】53. Maximum Subarray
- leetcode 53. Maximum Subarray
- LeetCode 53. Maximum Subarray
- LeetCode 53. Maximum Subarray
- leetcode:53. Maximum Subarray