1. /*
  2. * 327. Count of Range Sum
  3. * 2016-7-8 by Mingyang
  4. */
  5. public int countRangeSum(int[] nums, int lower, int upper) {
  6. int n = nums.length;
  7. long[] sums = new long[n + 1];
  8. for (int i = 0; i < n; ++i)
  9. sums[i + 1] = sums[i] + nums[i];
  10. return countWhileMergeSort(sums, 0, n + 1, lower, upper);
  11. }
  12. private int countWhileMergeSort(long[] sums, int start, int end, int lower, int upper) {
  13. if (end - start <= 1) return 0;
  14. int mid = (start + end) / 2;
  15. int count = countWhileMergeSort(sums, start, mid, lower, upper)
  16. + countWhileMergeSort(sums, mid, end, lower, upper);
  17. int j = mid, k = mid, t = mid;
  18. long[] cache = new long[end - start];
  19. for (int i = start, r = 0; i < mid; ++i, ++r) {
  20. while (k < end && sums[k] - sums[i] < lower) k++;
  21. while (j < end && sums[j] - sums[i] <= upper) j++;
  22. while (t < end && sums[t] < sums[i]) cache[r++] = sums[t++];
  23. cache[r] = sums[i];
  24. count += j - k;
  25. }
  26. System.arraycopy(cache, 0, sums, start, t - start);
  27. return count;
  28. }

327. Count of Range Sum的更多相关文章

  1. 【算法之美】你可能想不到的归并排序的神奇应用 — leetcode 327. Count of Range Sum

    又是一道有意思的题目,Count of Range Sum.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/leetcode ...

  2. [LeetCode] 327. Count of Range Sum 区间和计数

    Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...

  3. leetcode@ [327] Count of Range Sum (Binary Search)

    https://leetcode.com/problems/count-of-range-sum/ Given an integer array nums, return the number of ...

  4. 【LeetCode】327. Count of Range Sum

    题目: Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusiv ...

  5. 327. Count of Range Sum(inplace_marge)

    Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...

  6. 327 Count of Range Sum 区间和计数

    Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...

  7. LeetCode 327. Count of Range Sum

    无意看到的LeetCode新题,不算太简单,大意是给一个数组,询问多少区间和在某个[L,R]之内.首先做出前缀和,将问题转为数组中多少A[j]-A[i] (j>i)在范围内. 有一种基于归并排序 ...

  8. [LeetCode] Count of Range Sum 区间和计数

    Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...

  9. LeetCode Count of Range Sum

    原题链接在这里:https://leetcode.com/problems/count-of-range-sum/ 题目: Given an integer array nums, return th ...

随机推荐

  1. JAVA基础篇—继承

    父类Vehicle package com.car; public class Vehicle { final String brand;// String color;// double speed ...

  2. sqli-labs less1 &&less3&&less4学习心得

    0x01.less1 id=1/ id=1 and 1=1结果正常 id=1 and 1=2结果正常,不合理 id=1'提示:

  3. 《Scrum实战》第0次课【如何学习敏捷】全团课后任务汇总

    <Scrum实战>第0次课作业 完成情况: 课程名称:如何学习敏捷 1组 孟帅 孟帅: http://www.cnblogs.com/mengshuai1982/p/7096338.htm ...

  4. Spring学习总结(20)——Spring加载多个项目properties配置文件问题解决

    多数的鲜为人知方法都是因为有着罕见的应用,就比如说Spring中PropertyPlaceholderConfigurer这个类,它是用来解析Java Properties属性文件值,并提供在spri ...

  5. Xpath - Xpath定位

     selenium 提供的xpath定位方法名为:find_element_by_xpath(xpath表达式) Xpath基本定位语法: / 绝对定位,从根节点选取 // 相对定位,从匹配选择的当前 ...

  6. python - 字符串的格式化输出

    # -*- coding:utf-8 -*- '''@project: jiaxy@author: Jimmy@file: study_2_str.py@ide: PyCharm Community ...

  7. Andorid 生成NDK动态链接库 .so库

    .so库第一次见到是在搜索Android保存静态秘钥等特殊id字段做法时看到的-通过NDK的方式将静态秘钥保存在so文件中, 关于原生开发工具包(NDK)详细见官网指南要更详细,这里我记录我度娘各种结 ...

  8. 序列化 pickle & json & shelve

    把内存数据转成字符,叫序列化,dump,dumps 把字符转成内存数据类型,叫反序列化load,loads dumps:仅转成字符串 dump不仅能把对象转换成str,还能直接存到文件内 json.d ...

  9. 精通CSS高级Web标准解决方案(2-1 可视化格式模型之框模型)

    浮动.定位.框模型这些控制在页面上安排和显示元素的方式,形成CSS布局. 盒子模型 页面上的每个元素都被看成一个矩形框. 盒子模型有两种,分别是 IE 盒子模型和标准 W3C 盒子模型.他们对盒子模型 ...

  10. [python学习篇][廖雪峰][1]高级特性 ---迭代

    由于字符串也是可迭代对象,因此,也可以作用于for循环: >>> for ch in 'ABC': ... print ch ... A B C 所以,当我们使用for循环时,只要作 ...