Sliding window doesn't work. So it is a typical partial_sum base solution. As below. However if you use a balanced binary search tree, you can get O(nlgn) - like std::set<int>

class Solution {
public:
/**
* @param A an integer array
* @param start an integer
* @param end an integer
* @return the number of possible answer
*/
int subarraySumII(vector<int>& A, int start, int end) {
size_t len = A.size(); vector<int> presum(len + );
std::partial_sum(A.begin(), A.end(), presum.begin() + ); int ret = ;
for(int i = ; i <= len; i ++)
{
for(int j = ; j < i; j++)
{
int diff = presum[i] - presum[j];
if(diff<=end && diff>=start)
ret ++;
}
}
return ret;
}
};

LintCode "Subarray Sum II"的更多相关文章

  1. [LintCode] Subarray Sum & Subarray Sum II

    Subarray Sum Given an integer array, find a subarray where the sum of numbers is zero. Your code sho ...

  2. Continuous Subarray Sum II(LintCode)

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

  3. [LintCode] Continuous Subarray Sum II

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

  4. Lintcode: Subarray Sum 解题报告

    Subarray Sum 原题链接:http://lintcode.com/zh-cn/problem/subarray-sum/# Given an integer array, find a su ...

  5. Lintcode: k Sum II

    Given n unique integers, number k (1<=k<=n) and target. Find all possible k integers where the ...

  6. Lintcode: Subarray Sum Closest

    Given an integer array, find a subarray with sum closest to zero. Return the indexes of the first nu ...

  7. Subarray Sum II

    Description Given an positive integer array A and an interval. Return the number of subarrays whose ...

  8. Continuous Subarray Sum II

    Description Given an circular integer array (the next element of the last element is the first eleme ...

  9. LintCode Subarray Sum

    For this problem we need to learn a new trick that if your start sum up all elements in an array. Wh ...

随机推荐

  1. 应用Druid监控SQL语句的执行情况(转)

    Druid是什么? Druid首先是一个数据库连接池.Druid是目前最好的数据库连接池,在功能.性能.扩展性方面,都超过其他数据库连接池,包括DBCP.C3P0.BoneCP.Proxool.JBo ...

  2. 在虚拟上安装kali

    1.创建新的虚拟机 2.选择典型 下一步 3.选安装程序光盘映像文件:写入自己.iso的位置 4.linux--Ubuntu 5.给虚拟机名称 虚拟机在你电脑的位置 6.最大自盘自己定义 7.选自定义 ...

  3. 权限分配界面 纯手工 仅用到bootstrap的架构 以及 c标签

    <div class="form-group">        <div class="row">         <label ...

  4. form表单验证

    <script> $(document).ready(function(){ $('.pinglunform').submit(function(){ var issubmit = 0; ...

  5. JavaScript学习记录总结(五)——servlet将json数据写出去

    定义teacher和student实体 json.do   List<Student> stus=new ArrayList<Student>();        stus.a ...

  6. LB负载均衡集群及NAT模式配置

    一.LB(load balance)负载均衡集群 负载均衡集群常用的有: 1.软件实现的 nginx(工作在OSI第七层应用层) lvs+keepalived(工作在OSI第四层传输层) 2.硬件实现 ...

  7. 关于display的那些事儿!

    关于display的那些事儿! display,display,display!嘿嘿嘿!display这一CSS属性,还是相当神奇的哦!给它设置不同的值,被修饰的标签相应的就随之变换了自身的属性特性, ...

  8. 课堂所讲整理:输入输出流(I/O)

    package org.hanqi.ex; import java.io.*; public class TestFile { public static void main(String[] arg ...

  9. 越狱Season 1- Episode 18: Bluff

    Season 1, Episode 18: Bluff -Michael: Scofield Scofield Michael Scofield Michael Scofield -Patoshik: ...

  10. ps命令详解(转)

    原文地址:http://apps.hi.baidu.com/share/detail/32573968 有时候系统管理员可能只关心现在系统中运行着哪些程序,而不想知道有哪些进程在运行.由于一个应用程序 ...