求数组中任意两个数之间所有数字的和

303. Range Sum Query - Immutable

  求数组中任意两个数之间所有数字的和

QuestionEditorial Solution

My Submissions

  • Total Accepted: 37248
  • Total Submissions: 146945
  • Difficulty: Easy

Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.

Example:

Given nums = [-2, 0, 3, -5, 2, -1]

sumRange(0, 2) -> 1
sumRange(2, 5) -> -1
sumRange(0, 5) -> -3

Note:

  1. You may assume that the array does not change.
  2. There are many calls to sumRange function.

Subscribe to see which companies asked this question

 1 // 存储数组的结构体
 2 struct NumArray {
 3     int num;
 4     int sum;
 5 };
 6
 7 /** Initialize your data structure here. */
 8 struct NumArray* NumArrayCreate(int* nums, int numsSize) {
 9     int i = 0;
10     struct NumArray *NumArr = (struct NumArray*)malloc(sizeof(struct NumArray)*numsSize);//数组结构体初始化
11     struct NumArray* p = NumArr;
12
13     //非空数组
14     if(numsSize == 0)
15         return NULL;
16
17     //初始化第一个元素
18     p[0].num = nums[0];
19     p[0].sum = nums[0];
20     i++;
21     while(i < numsSize){
22         p[i].num = nums[i];
23         p[i].sum = p[i-1].sum + nums[i];
24         i++;
25     }
26
27     return NumArr;
28 }
29
30 int sumRange(struct NumArray* numArray, int i, int j) {
31     //sum[j] -sum[i] + num[i]
32     return (numArray[j].sum - numArray[i].sum + numArray[i].num);
33 }
34
35 /** Deallocates memory previously allocated for the data structure. */
36 void NumArrayFree(struct NumArray* numArray) {
37     free(numArray);
38 }
39
40 // Your NumArray object will be instantiated and called as such:
41 // struct NumArray* numArray = NumArrayCreate(nums, numsSize);
42 // sumRange(numArray, 0, 1);
43 // sumRange(numArray, 1, 2);
44 // NumArrayFree(numArray);

时间: 2024-08-08 21:51:39

求数组中任意两个数之间所有数字的和的相关文章

[BC]求数组中任意两个数的公约数,,多种情况取最大值

#include <iostream> #include <stdio.h> #include <stdlib.h> #include <algorithm> #include <string.h> using namespace std; int main() { int t,i,c,j,n,x,max,k; int a[100005]; scanf("%d",&t); for(j=1;j<=t;j++) {

在O(n)时间复杂度内求无序数组中任意两个元素的最大差值,以及存在的组数

题目描述: 求无序数组中任意两个元素的最大差值,以及存在最大差值的组别数. 输入: 输入包含两行,第一行输入一个整数n:第二行n个正整数,用空格隔开. 输出: 输出为一行,包含最大差值,以及存在组别数. 样例输入: 4 4  1  2  1 输出: 3  2 一种实现代码如下(Java版): 1 import java.util.Scanner; 2 /** 3 * 在O(n)时间复杂度内求无序数组中任意两个元素的最大差值,以及存在的组数 4 * @author JiaJoa 5 * 6 */

输入一个有序数组和一个数字,在数组中查找两个数,使得它们的和正好是输入的那个数字

输入一个有序数组和一个数字,在数组中查找两个数,使得它们的和正好是输入的那个数字.如果有多对数字的和等于输入的数字,输出任意一对即可.例如输入数组1.2.4.7.11.15和数字15.由于4+11=15,因此输出4和11. 1 #include<stdio.h> 2 #include<stdlib.h> 3 4 void findTwo(int *array, int len, int sum) 5 { 6 int beg = 0; 7 int end = len-1; 8 int

14.输入一个已经按升序排序过的数组和一个数字, 在数组中查找两个数,使得它们的和正好是输入的那个数字。 要求时间复杂度是O(n)

待完善! 转载请注明出处:http://www.cnblogs.com/wuzetiandaren/p/4259199.html 声明:现大部分文章为寻找问题时在网上相互转载,此博是为自己做个记录记录,方便自己也方便有类似问题的朋友,本文的思想也许有所借鉴,但源码均为本人实现,如有侵权,请发邮件表明文章和原出处地址,我一定在文章中注明.谢谢. 题目: 输入一个已经按升序排序过的数组和一个数字,在数组中查找两个数,使得它们的和正好是输入的那个数字.要求时间复杂度是O(n).如果有多对数字的和等于输

输入一个已经按升序排序的数组和一个数字 ,在数组中查找两个数,使得他们的和是输入的那个数字

package shuzu; /* * 输入一个已经按升序排序的数组和一个数字 * 在数组中查找两个数,使得他们的和是输入的那个数字,要求时间复杂度为o(n) * 如果有多对数字的和等于输入的数字,输出任意一对即可. */ public class demo1 { private static void findAns(int[] data,int sum) { int size=data.length; int begin =0; int end=size-1; while(begin < s

bestcoder#43 1002 在数组中找两个数的和取模的最大值 二分

bestcoder#43 1002 在数组中找两个数的和取模的最大值  二分 pog loves szh II Accepts: 97 Submissions: 834 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem Description Pog and Szh are playing games. There is a sequence with n number

【编程题目】输入一个已经按升序排序过的数组和一个数字,在数组中查找两个数,使得它们的和正好是输入的那个数字。

第 14 题(数组):题目:输入一个已经按升序排序过的数组和一个数字,在数组中查找两个数,使得它们的和正好是输入的那个数字.要求时间复杂度是 O(n).如果有多对数字的和等于输入的数字,输出任意一对即可.例如输入数组 1.2.4.7.11.15 和数字 15.由于 4+11=15,因此输出 4 和 11. 要求时间是O(n)肯定就只能扫描一遍. 又有两个数字要找,那就只能一个从头向后找 一个从后向前找 初始把大值设为最后一个数, 小值设为第一个数,如果数字和大于和,则减小大数的数值, 反之增大小

求二叉树中任意两个结点的距离

求二叉树中任意两个结点的距离 实现步骤: 计算跟到第一个结点的距离: 计算跟到第二个结点的距离: 计算lca: 计算跟到lca结点的距离: 结果为(1) + (2) - 2 * (4),因为重复计算了两次的从跟到lca结点的距离: 1 class Node(object): def __init__(self, value=0): self.value = value self.left = self.right = None def get_path_length(root, n, path)

1.2、和一定时找数组中的两个数(2)

题目同1.和一定时找数组中的两个数 程序差别:此次用三个if语句进行指针的移动. 代码: public class one2 { public static void main(String[] args) { int[] nums = {1,2,3,4,5,6,7,8,9}; int target = 10; int a=0; int b=1; while (a<b) if (a+b==target) { System.out.println("There have"+nums