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. Think Python - Chapter 17 - Classes and methods

    17.1 Object-oriented featuresPython is an object-oriented programming language, which means that it ...

  2. ZOJ 1201 Inversion

    原题链接 题目大意:给一个数组{ A1,A2,…,An } ,要求生成另一个数组B1,B2,…,Bn,使得Bi表示的是在数组A中排在i前面大于i的数字的个数.题目的输入是数组A(字母P表示)或者数组B ...

  3. Integer to Roman

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

  4. leetcode之链表排序题

    原文链接:点击打开链接 原题是这样的: Given a linked list and a value x, partition it such that all nodes less than x  ...

  5. 基础套接字的C#网络编程

    1.基于socket创建套接字网络连接服务端1.初始化 步骤 操作 方法 操作类 1. 创建ip ipaddress IPAddress类 2. 创建ip终结点   ipendpoint IpendP ...

  6. leetcode 123. Best Time to Buy and Sell Stock III ----- java

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  7. nginx无法启动: libpcre.so.1/libpcre.so.0: cannot open shared object file解决办法

    NGINX启动时提示错误: /usr/local/nginx/sbin/nginx -t/usr/local/nginx/sbin/nginx: error while loading shared ...

  8. Unity资源管理与更新

    当你在 工程目录下的 Asset 文件夹中放置一个文件时(电脑上的文件夹,不是 Unity 界面中的文件夹),Unity会自动检测到一个新的文件被添加(Unity会不停地检查Assets文件夹中的内容 ...

  9. Android中突发情况Activity数据的保存和恢复

    Android中突发情况Activity数据的保存和恢复 写在前面:在我们的APP使用的过程中,总有可能出现各种手滑.被压在后台.甚至突然被杀死的情况.所以对APP中一些临时数据或关键持久型数据,就需 ...

  10. ASP.NET 各种缓存

    原文地址:http://www.cnblogs.com/ltp/archive/2009/06/30/1514311.html   有时候总听到网友说网站运行好慢,不知如何是好:有时候也总见到一些朋友 ...