[leetcode] Maximum Subarray
2015-06-24 19:00
381 查看
From : https://leetcode.com/problems/maximum-subarray/
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
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.
class Solution { public: int maxSubArray(vector<int>& nums) { int ans = nums[nums.size()-1], pre=ans; for(int i=nums.size()-2; i>=0; i--) { pre = nums[i] + (pre>0)*pre; if(pre > ans) ans=pre; } return ans; } };
相关文章推荐
- 策略模式
- android studio 导入第三方库的方法
- HttpClient Post请求
- [Effective C++] 读书笔记 I 书读百遍,其义自现
- An internal error occurred during: "Building workspace". GC overhead limit exceeded
- make命令重定向
- Android 电话和短信拦截
- An internal error occurred during: "Building workspace". GC overhead limit exceeded
- 经典网址
- 一致性哈希
- [LeetCode] Regular Expression Matching
- redis学习第一天
- jQuery入门:jQuery是如何工作的 How jQuery Works
- Android学习笔记
- Jquery-获取iframe中的dom对象
- NASM Intro - Compile and Link
- Android上传图片到服务器
- Bootstrap 分页插件 ajax获取数据显示
- vmware下ubuntu虚拟机与宿主机共享文件夹
- Android图片压缩