[LeetCode]303. Range Sum Query
来源:互联网 发布:怎么开个淘宝网店代理 编辑:程序博客网 时间:2024/06/06 00:19
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) -> 1sumRange(2, 5) -> -1sumRange(0, 5) -> -3
Note:
- You may assume that the array does not change.
- There are many calls to sumRange function.
思路:很简单,用一个同等长度的数组存到i元素的所有和,返回range的时候就用sum[j]-sum[i-1]
public class NumArray { int[] sum; public NumArray(int[] nums) { sum=new int[nums.length]; int s=0; for(int i=0;i<nums.length;i++){ s+=nums[i]; sum[i]=s; } } public int sumRange(int i, int j) { if(i>0){ return sum[j]-sum[i-1]; }else{ return sum[j]; } }}/** * Your NumArray object will be instantiated and called as such: * NumArray obj = new NumArray(nums); * int param_1 = obj.sumRange(i,j); */
0 0
- 303.[LeetCode]Range Sum Query
- LeetCode-303. Range Sum Query
- [LeetCode]303. Range Sum Query
- LeetCode#303. Range Sum Query
- leetcode 303. Range Sum Query
- [LeetCode]303. Range Sum Query
- LeetCode 303. Range Sum Query
- LeetCode 303. Range Sum Query
- LeetCode 303. Range Sum Query
- leetcode-303. Range Sum Query
- LeetCode 303. Range Sum Query
- leetcode.303.Range Sum Query
- leetcode 303. Range Sum Query
- leetcode 303. Range Sum Query
- [leetcode]: 303. Range Sum Query
- [Leetcode] 303. Range Sum Query
- [LeetCode]303. Range Sum Query
- leetcode 303. Range Sum Query
- Maximum Depth of Binary Tree
- POJ 3268 Silver Cow Party (单源最短路Dijkstra+反向构图)
- 深入浅出解析大数据平台架构
- 2017.04.25 记雏鹰博客系统开工日。
- selinux对文件的控制
- [LeetCode]303. Range Sum Query
- Spring MVC优势和第一个入门程序
- LAMP部署成功后访问http://localhost:80/
- LeetCode | 26. Remove Duplicates from Sorted Array
- 使用pandas中的DataFrame数据绘制柱状图
- [LeetCode]304. Range Sum Query 2D
- Redis容灾部署哨兵(sentinel)机制配置详解及原理介绍
- Typora学习笔记
- C# 在屏幕上画图效果的经典例子