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"的更多相关文章

  1. [LintCode] Continuous Subarray Sum II

    Given an integer array, find a continuous rotate subarray where the sum of numbers is the biggest. Y ...

  2. [LintCode] Continuous Subarray Sum 连续子数组之和

    Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your cod ...

  3. LintCode 402: Continuous Subarray Sum

    LintCode 402: Continuous Subarray Sum 题目描述 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的下标 ...

  4. Continuous Subarray Sum II(LintCode)

    Continuous Subarray Sum II   Given an circular integer array (the next element of the last element i ...

  5. 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题是存储的当前累加和的 ...

  6. Continuous Subarray Sum

    Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your cod ...

  7. [LeetCode] Continuous Subarray Sum 连续的子数组之和

    Given a list of non-negative numbers and a target integer k, write a function to check if the array ...

  8. [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 ...

  9. Continuous Subarray Sum LT523

    Given a list of non-negative numbers and a target integer k, write a function to check if the array ...

随机推荐

  1. 关于IE6浮动问题!

    以下内容均为个人笔记:望批评指教! IE6下应尽量少使用float 而是  换用display:inline  父层使用text-align:text:效果会好些: 如果一组浮动元素 产生了浮动 最好 ...

  2. Context启动startActivity注意

    intent.setFlags()方法中参数的用例: 很多人使用startActivity时候,会碰到如下的异常:Caused by: android.util.AndroidRuntimeExcep ...

  3. Foobar音乐播放器——最佳音乐播放器 - imsoft.cnblogs

    简单皮肤 下载地址:链接: http://pan.baidu.com/s/1kTwn2dh 密码: lf5f

  4. MATLAB格式化输出控制

    format 默认格式 format short 5字长定点数 format long 15字长定点数 format short e 5字长浮点数 format long e 15字长浮点数 form ...

  5. scala言语基础学习五

    extends override 和super方法 override field 父类不是val对象不能覆盖field isInstanceOf和asInstanceOf(isInstanceOf是用 ...

  6. Hardly Hard

    You have been given the task of cutting out a quadrilateral slice of cake out of a larger, rectangul ...

  7. 一些基本的C/C++数据类型

    size_t size_t. A basic unsigned integer C/C++ type. It is the type of the result returned by sizeof ...

  8. Android项目——电话拨号器

    因为应用要使用手机的电话服务,所以要在清单文件AndroidManifest.xml中添加电话服务权限: <?xml version="1.0" encoding=" ...

  9. Android sdk 镜像服务器资源

    大连东软信息学院镜像服务器地址:- http://mirrors.neusoft.edu.cn 端口:80北京化工大学镜像服务器地址:- IPv4: http://ubuntu.buct.edu.cn ...

  10. breakpoints

    https://blogs.msdn.microsoft.com/visualstudioalm/2013/10/07/breakpoints-in-visual-studio-2013/ Using ...