LintCode "Continuous Subarray Sum"
A variation to a classical DP: LCS.
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) {
vector<int> ret; size_t len = A.size(); int curr = A[];
int sum = A[], csum = A[];
int s = , e = , ss = , ee = ;
for (int i = ; i < len; i++)
{
int nsum = csum + A[i];
if (A[i] > nsum)
{
ss = ee = i;
csum = A[i];
}
else
{
ee = i;
csum = nsum;
} if(csum > sum) // update global record
{
sum = nsum;
s = ss;
e = ee;
}
} ret.push_back(s);
ret.push_back(e);
return ret;
}
};
LintCode "Continuous Subarray Sum"的更多相关文章
- [LintCode] Continuous Subarray Sum II
Given an integer array, find a continuous rotate subarray where the sum of numbers is the biggest. Y ...
- [LintCode] Continuous Subarray Sum 连续子数组之和
Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your cod ...
- LintCode 402: Continuous Subarray Sum
LintCode 402: Continuous Subarray Sum 题目描述 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的下标 ...
- Continuous Subarray Sum II(LintCode)
Continuous Subarray Sum II Given an circular integer array (the next element of the last element i ...
- leetcode 560. Subarray Sum Equals K 、523. Continuous Subarray Sum、 325.Maximum Size Subarray Sum Equals k(lintcode 911)
整体上3个题都是求subarray,都是同一个思想,通过累加,然后判断和目标k值之间的关系,然后查看之前子数组的累加和. map的存储:560题是存储的当前的累加和与个数 561题是存储的当前累加和的 ...
- Continuous Subarray Sum
Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your cod ...
- [LeetCode] Continuous Subarray Sum 连续的子数组之和
Given a list of non-negative numbers and a target integer k, write a function to check if the array ...
- [Swift]LeetCode523. 连续的子数组和 | Continuous Subarray Sum
Given a list of non-negative numbers and a target integer k, write a function to check if the array ...
- Continuous Subarray Sum LT523
Given a list of non-negative numbers and a target integer k, write a function to check if the array ...
随机推荐
- 如何用 freebayes call SNP
1,软件介绍 FreeBayes is a Bayesian genetic variant detector designed to find small polymorphisms, specif ...
- Indexof
注释:indexOf() 方法对大小写敏感! 注释:如果要检索的字符串值没有出现,则该方法返回 -1. 实例 在本例中,我们将在 "Hello world!" 字符串内进行不同的检 ...
- 最大连续子序列和问题(Maximum Consecutive Subsequence Sum)
该算法的定义是:给出一个int序列,元素有正有负,找出其中的最大连续子序列的和. 例如:-2,11,-4,13,-5-2,:最大和为20(11,-4, 13). 怎么考虑这个问题呢? 要充分利用,连续 ...
- leetcode 128. Longest Consecutive Sequence ----- java
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- 英语语法最终珍藏版笔记-6“情态动词+have+ done”的含义
“情态动词+have+ done”的含义 1.Must have done的含义.“must have+过去分词”表示对过去的推测,意思是“一定已经,想必已经,准是已经….”,只用于肯定句中.例如: ...
- IE 下加载jQuery
转:http://www.iitshare.com/ie8-not-use-native-json.html 解决在IE8中无法使用原生JSON的问题 起因 在项目中要将页面上的js对象传给后台, ...
- MySQL root密码重置 报错:mysqladmin: connect to server at 'localhost' failed的解决方案
===========================================================二,忘记本地root的登录密码解决过程:1.编辑/mysql/my.ini在[my ...
- JVM ,JIT ,GC RUNTIME 解析
Java Class字节码知识点回顾 https://yq.aliyun.com/articles/2358?spm=5176.8067842.tagmain.105.fQdvH3 JVM Class ...
- VC++ 0xC0000005: Access violation.
public: COptionDlg(CWnd* pParent = NULL); // 标准构造函数 virtual ~COptionDlg(); TCONFIG m_tCfg; // 对话框数据 ...
- 旋转camera到特定对象
设定一个物体使得camera可以从现在为止自动飞到当前位置 1. 设定一个位置,可以在其前方放置一个显示其位置的cube.这里也可以写脚本设定位置. 2. 使用函数 移动函数 transform.po ...