53. Maximum Subarray
2016-03-28 10:52
197 查看
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.
For example, given the array
the contiguous subarray
看《剑指offer》看到这道题,过来刷一下···
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.
看《剑指offer》看到这道题,过来刷一下···
class Solution { public: int maxSubArray(vector<int>& nums) { int cursum=0; int greatestsum=0x80000000; //可以表示的最大的负数 for(int i=0;i<nums.size();i++){ if(cursum<=0) //如果cursum为负,如果算上cursum会小于不算cursum开始的子数组的和,因此不需要前面的子数组,cursum扔掉 cursum=nums[i]; else cursum+=nums[i]; if(cursum>greatestsum) greatestsum=cursum; } return greatestsum; } };
相关文章推荐
- 判断元素是否绑定了事件
- Gradle build.gradle to Maven pom.xml
- myeclipse 常用快捷键
- 探究chrome下的开发工具的各功能
- 24款最好的jQuery日期时间选择器插件
- MFC 创建多级文件目录
- 查询功能:动态的添加列名和条件
- Binary XML file line #8: Error inflating class android.support.design.widget.NavigationView
- 目标检测的框架与过程
- HDU 5651 xiaoxin juju needs help 逆元&组合数学
- category添加属性
- 今天开始学模式识别与机器学习Pattern Recognition and Machine Learning (PRML)书,章节1.2,Probability Theory (上)
- Android NDK R9
- dom4j 遍历所有元素和属性
- 张老师:4.传统线程同步通讯技术
- eval函数的工作原理
- mysql给root开启远程访问权限
- 移动端6种导航设计总结
- 玩转iOS卡片布局
- [Learn Android Studio 汉化教程]第一章 : Android Studio 介绍