Given an integer array (index from 0 to n-1, where n is the size of this array), and an query list. Each query has two integers [start, end]. For each query, calculate the sum number between index start and end in the given array, return the result list.

Have you met this question in a real interview? Yes
Example
For array [1,2,7,8,5], and queries [(0,4),(1,2),(2,4)], return [23,9,20] Note
We suggest you finish problem Segment Tree Build, Segment Tree Query and Segment Tree Modify first. Challenge
O(logN) time for each query

这道题最简便的方法当然是prefix Sum

/**
* Definition of Interval:
* public classs Interval {
* int start, end;
* Interval(int start, int end) {
* this.start = start;
* this.end = end;
* }
*/
public class Solution {
/**
*@param A, queries: Given an integer array and an query list
*@return: The result list
*/
public ArrayList<Long> intervalSum(int[] A,
ArrayList<Interval> queries) {
// write your code here
long[] prefixSum = new long[A.length+1];
for (int i=0; i<A.length; i++) {
prefixSum[i+1] = prefixSum[i] + A[i];
}
ArrayList<Long> res = new ArrayList<Long>();
for (Interval interval : queries) {
int start = interval.start;
int end = interval.end;
long result = prefixSum[end+1] - prefixSum[start];
res.add(result);
}
return res;
}
}

用Segment Tree

 /**
* Definition of Interval:
* public classs Interval {
* int start, end;
* Interval(int start, int end) {
* this.start = start;
* this.end = end;
* }
*/
public class Solution {
/**
*@param A, queries: Given an integer array and an query list
*@return: The result list
*/
public class SegmentTreeNode {
long sum;
int start;
int end;
SegmentTreeNode left;
SegmentTreeNode right;
public SegmentTreeNode(int start, int end) {
this.sum = 0;
this.start = start;
this.end = end;
this.left = null;
this.right = null;
}
} SegmentTreeNode root; public ArrayList<Long> intervalSum(int[] A,
ArrayList<Interval> queries) {
// write your code here
ArrayList<Long> res = new ArrayList<Long>();
root = build(A, 0, A.length-1);
for (Interval interval : queries) {
int start = interval.start;
int end = interval.end;
res.add(query(root, start, end));
}
return res;
} public SegmentTreeNode build(int[] A, int start, int end) {
SegmentTreeNode cur = new SegmentTreeNode(start, end);
if (start == end) {
cur.sum = A[start];
}
else {
int mid = (start + end)/2;
cur.left = build(A, start, mid);
cur.right = build(A, mid+1, end);
cur.sum = cur.left.sum + cur.right.sum;
}
return cur;
} public Long query(SegmentTreeNode cur, int start, int end) {
if (cur.start==start && cur.end==end) return cur.sum;
int mid = (cur.start + cur.end)/2;
if (end <= mid) return query(cur.left, start, end);
else if (start > mid) return query(cur.right, start, end);
else return query(cur.left, start, mid) + query(cur.right, mid+1, end);
}
}

Lintcode: Interval Sum的更多相关文章

  1. Lintcode: Interval Sum II

    Given an integer array in the construct method, implement two methods query(start, end) and modify(i ...

  2. Interval Sum I && II

    Given an integer array (index from 0 to n-1, where n is the size of this array), and an query list. ...

  3. lintcode: k Sum 解题报告

    K SUM My Submissions http://www.lintcode.com/en/problem/k-sum/ 题目来自九章算法 13% Accepted Given n distinc ...

  4. LintCode "4 Sum"

    4 Pointer solution. Key: when moving pointers, we skip duplicated ones. Ref: https://github.com/xbz/ ...

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

  6. Lintcode: Subarray Sum 解题报告

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

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

  8. [LintCode] Two Sum 两数之和

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  9. [LintCode] Submatrix Sum 子矩阵之和

    Given an integer matrix, find a submatrix where the sum of numbers is zero. Your code should return ...

随机推荐

  1. 微信公众账号开发教程(一) 基本原理及微信公众账号注册 ——转自http://www.cnblogs.com/yank/p/3364827.html

    微信公众账号开发教程 基本原理 在开始做之前,大家可能对这个很感兴趣,但是又比较茫然.是不是很复杂?很难学啊? 其实恰恰相反,很简单.为了打消大家的顾虑,先简单介绍了微信公众平台的基本原理. 微信服务 ...

  2. android文字阴影效果(转)

    关于android文字阴影,共有四个属性可以设置: android:shadowColor :阴影颜色 android:shadowDx :阴影x方向位移 android:shadowDy :阴影y方 ...

  3. bug

    expected identifier,string or number   //这种问题一般是json数据中最后一个逗号没去掉.

  4. session和cookie区别

    <?php session_start(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//E ...

  5. 1014 C语言文法

    <程序> -> <外部声明> | <程序> <外部声明> <外部声明> -> <函数定义> | <声明> ...

  6. 通过runtime替换系统类实现的代码(从github开源库fdstackview中摘录)

    其中部分代码为汇编:由此可见oc的runtime的灵活性和能力.此代码仅供参考 // ---------------------------------------------------- // R ...

  7. Java高级之类结构的认识

    本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! Java体系包括,各种版本的虚拟机,可执行文件,各种第三方类库,Java API类库,Java语言 ...

  8. JQuery源码之“对象的结构解析”

    吃完午饭,觉得有点发困,想起了以后我们的产品可能要做到各种浏览器的兼容于是乎不得不清醒起来!我们的web项目多数是依赖于Jquery的.据了解,在Jquery的2.0版本以后对IE的低端版本浏览器不再 ...

  9. 行高不设单位的好处 line-height:1.8

    今天无意间看了到了line-height:1.8 感觉挺有意思的,然后翻了下行高的知识,发现还挺有文章的,不妨温故而知新. 先回顾下:顶线.中线.基线.底线 vertical-align是元素的垂直对 ...

  10. JavaScript操作数组

    数组被描述为一个存储元素的线性集合,元素可以通过索引来任意存取. 几乎所有的编程语言都有类似的数据结构,但是Javascript中的数组却略有不同. Javascript中的数组是一种特殊的对象,所以 ...