Maximum Subarray
2015-10-10 16:31
225 查看
</pre><pre name="code" class="cpp">int max(int a,int b) { return a>=b?a:b; } int maxSubArray(int* nums, int numsSize) { int sum = nums[0] , maxSum = nums[0]; int i=0; for(i = 1; i < numsSize; i++){ if(sum < 0) sum = 0; sum += nums[i]; maxSum = max(maxSum, sum); } return maxSum; }
Find 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.
相关文章推荐
- 采用Sambaserver由win平台,linux平台上传文件
- Java调取底层命令获取主机名(localhostName)
- shell echo命令介绍
- HDU 5228 ZCC loves straight flush
- 程序在Windows上运行得很慢,怎么判别是程序存在问题还是软硬件系统存在问题?
- 360儿童卫士
- 重力传感器、加速度传感器以及陀螺仪的区别
- Javascript输出
- Nginx 499错误的原因及解决方法
- 网站测试总结
- Java_Map集合
- mysql sql语句为表批量增加字段
- 1. VS2010---简介
- JavaScript拼接字符串传递多个参数
- 位运算 UEST 84 Binary Operations
- Android使用TextView实现无下划线超链接
- 微信端开发研究与技术调研(二)
- Android 图片加载和缓存库 Glide
- jni简介
- poj2367 拓扑排序