[LintCode] Continuous Subarray Sum
来源:互联网 发布:网络女神成路人 贴吧 编辑:程序博客网 时间:2024/05/16 15:07
Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your code should return the index of the first number and the index of the last number. (If their are duplicate answer, return anyone)
For this problem, we first need to track subarray’s start point, and the maxSum. O(n) time complexity.
class Solution {public: /** * @param A an integer array * @return A list of integers includes the index of * the first number and the index of the last number */ vector<int> continuousSubarraySum(vector<int>& A) { // Write your code here vector<int> res(2,-1); if(A.empty()) return res; int maxSum = INT_MIN, sum = 0, start = 0, end = 0; for(int i = 0; i<A.size(); ++i){ sum += A[i]; if(sum > maxSum){ maxSum = sum; res[0] = start; res[1] = i; } if(sum<0) { start = i+1; sum = 0; } } return res; }};
0 0
- lintcode:Continuous Subarray Sum
- [LintCode] Continuous Subarray Sum
- [Lintcode] Continuous Subarray Sum
- lintcode-Continuous Subarray Sum
- [LintCode] Continuous Subarray Sum II
- Continuous Subarray Sum II
- Continuous Subarray Sum
- 【LeetCode523】. Continuous Subarray Sum
- [LeetCode]Continuous Subarray Sum
- 题解:Continuous Subarray Sum
- 523. Continuous Subarray Sum
- 523. Continuous Subarray Sum
- 523. Continuous Subarray Sum
- leetcode523. Continuous Subarray Sum
- 523. Continuous Subarray Sum
- Continuous Subarray Sum
- 523. Continuous Subarray Sum
- 523. Continuous Subarray Sum
- Scrollview 嵌套 RecyclerView 及在Android 5.1版本滑动时 惯性消失问题
- Java:Java2D高级绘图
- Android版本进化史
- 统计信息
- LeetCode Two Sum
- [LintCode] Continuous Subarray Sum
- UFT常识
- 移动web知识点
- truncate/drop表非常慢,怎么办?用硬链接,极速体验
- R语言sem包实现通径分析
- android查看剩余电量
- APNS远程推送证书的申请和制作——详细解析
- 一起来了解下java的Buffer!
- iOS开发系列--通讯录、蓝牙、内购、GameCenter、iCloud、Passbook系统服务开发汇总