LintCode "Subarray Sum II"
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"的更多相关文章
- [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 ...
- Continuous Subarray Sum II(LintCode)
Continuous Subarray Sum II Given an circular integer array (the next element of the last element i ...
- [LintCode] Continuous Subarray Sum II
Given an integer array, find a continuous rotate subarray where the sum of numbers is the biggest. Y ...
- Lintcode: Subarray Sum 解题报告
Subarray Sum 原题链接:http://lintcode.com/zh-cn/problem/subarray-sum/# Given an integer array, find a su ...
- Lintcode: k Sum II
Given n unique integers, number k (1<=k<=n) and target. Find all possible k integers where the ...
- Lintcode: Subarray Sum Closest
Given an integer array, find a subarray with sum closest to zero. Return the indexes of the first nu ...
- Subarray Sum II
Description Given an positive integer array A and an interval. Return the number of subarrays whose ...
- Continuous Subarray Sum II
Description Given an circular integer array (the next element of the last element is the first eleme ...
- 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 ...
随机推荐
- MySQL 添加列,修改列,删除列
创建后表的修改 alter table 语句用于创建后对表的修改, 基础用法如下: 添加列 基本形式: alter table 表名 add 列名 列数据类型 [after 插入位置]; 示例: 在表 ...
- Java多线程的实现
记得面试的时候,面试官问了Java多线程实现的方式有几种,它们之间的区别是什么?作为一个Java新手,将最近的学习总结如下: 1.Java多线程实现方式 Java多线程实现方式主要有三种:继承Thre ...
- java script 闭包
闭包的概念真的是很绕,我就来点实际的代码. 当我用下面的代码的时候 发生了闭包,当执行onclick事件的时候,变量一直引用了外部函数的变量,结果i总是4 function newLoad() { / ...
- 关于C中scanf()函数读取字符串的问题
#include <stdio.h> int main(void) { ]; scanf("%s", s_name); printf("Hello, %s!\ ...
- jq中的三元运算结构
三元运算的结构为:Boolean?值1:值2.它的第一个参数必须为布尔值.
- leetcode 144. Binary Tree Preorder Traversal ----- java
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- Android——ListView
1.ArryAdapter: arry_adapter的layout文件: <?xml version="1.0" encoding="utf-8"?&g ...
- JAVA导入包
在package **;下面写入 import java.**.**; 1.使用Scanner工具类来获取用户输入的成绩信息 Scanner类位于java.util包中,使用时需要导入此包 1.1. ...
- 如何更新Android SDK和Build Tool
1. 运行命令 android 2. 勾选并安装需要的版本 3. 完成!
- Android Studio 常用快捷键以及设置
常用快捷键: Ctrl+Q 出现文档提示 跟ecplise的 鼠标悬浮差不多 Ctrl+Alt+t 包围代码 Home End 移动光标到文本首和文本尾 Alt+回车 导入当前包 Ctrl+Alt+O ...