zoukankan      html  css  js  c++  java
  • Lintcode: Interval Sum II

    Given an integer array in the construct method, implement two methods query(start, end) and modify(index, value):
    
    For query(start, end), return the sum from index start to index end in the given array.
    For modify(index, value), modify the number in the given index to value
    Have you met this question in a real interview? Yes
    Example
    Given array A = [1,2,7,8,5].
    
    query(0, 2), return 10.
    modify(0, 4), change A[0] from 1 to 4.
    query(0, 1), return 6.
    modify(2, 1), change A[2] from 7 to 1.
    query(2, 4), return 14.
    Note
    We suggest you finish problem Segment Tree Build, Segment Tree Query and Segment Tree Modify first.
    
    Challenge
    O(logN) time for query and modify.

    Segment Tree:

     1 public class Solution {
     2     /* you may need to use some attributes here */
     3     class SegmentTreeNode {
     4         long sum;
     5         int start;
     6         int end;
     7         SegmentTreeNode left;
     8         SegmentTreeNode right;
     9         SegmentTreeNode(int start, int end) {
    10             this.sum = 0;
    11             this.start = start;
    12             this.end = end;
    13             this.left = null;
    14             this.right = null;
    15         }
    16     }
    17     
    18     SegmentTreeNode root;
    19 
    20     /**
    21      * @param A: An integer array
    22      */
    23     public Solution(int[] A) {
    24         // write your code here
    25         if (A == null || A.length==0) return;
    26         root = build(A, 0, A.length-1);
    27         
    28     }
    29     
    30     public SegmentTreeNode build(int[] A, int start, int end) {
    31         SegmentTreeNode cur = new SegmentTreeNode(start, end);
    32         if (start == end) cur.sum = A[start];
    33         else {
    34             int mid = (start + end)/2;
    35             cur.left = build(A, start, mid);
    36             cur.right = build(A, mid+1, end);
    37             cur.sum = cur.left.sum + cur.right.sum;
    38         }
    39         return cur;
    40     }
    41     
    42     /**
    43      * @param start, end: Indices
    44      * @return: The sum from start to end
    45      */
    46     public long query(int start, int end) {
    47         // write your code here
    48         return queryTree(root, start, end);
    49     }
    50     
    51     public long queryTree(SegmentTreeNode cur, int start, int end) {
    52         if (cur.start==start && cur.end==end) return cur.sum;
    53         int mid = (cur.start + cur.end)/2;
    54         if (end <= mid) return queryTree(cur.left, start, end);
    55         else if (start > mid) return queryTree(cur.right, start, end);
    56         else return queryTree(cur.left, start, mid) + queryTree(cur.right, mid+1, end);
    57     }
    58     
    59     /**
    60      * @param index, value: modify A[index] to value.
    61      */
    62     public void modify(int index, int value) {
    63         // write your code here
    64         modifyTree(root, index, value);
    65     }
    66     
    67     public void modifyTree(SegmentTreeNode cur, int index, int val) {
    68         if (cur.start == cur.end) {
    69             cur.sum = val;
    70             return;
    71         }
    72         int mid = (cur.start + cur.end)/2;
    73         if (index <= mid) modifyTree(cur.left, index, val);
    74         else modifyTree(cur.right, index, val);
    75         cur.sum = cur.left.sum + cur.right.sum;
    76     }
    77 }
  • 相关阅读:
    富文本编辑器layedit,调用setContent方法会报错
    sqlserver2008事务日志已满
    解决asp.net上传文件时文件太大导致的错误
    完美版js金钱正则表达式校验
    jQuery实现清空table表格除首行外的所有数据
    textArea中的maxlength是无效的 解决办法
    jquery根据name属性查找
    fileupload页面跳转找不到原页面的解决方法
    xml获取属性值的方法
    读FCL源码系列之List<T>---让你知其所以然---内含疑问求大神指点
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/5176677.html
Copyright © 2011-2022 走看看