leetcode 303. 区域和检索 - 数组不可变
作者:互联网
1 class NumArray { 2 public int[] prefixSum; 3 public NumArray(int[] nums) { 4 prefixSum = new int[nums.length]; 5 prefixSum[0] = nums[0]; 6 for(int i = 1;i < nums.length;i++) 7 prefixSum[i] = prefixSum[i - 1] + nums[i]; 8 } 9 10 public int sumRange(int left, int right) { 11 return left == 0 ? prefixSum[right] : prefixSum[right] - prefixSum[left - 1]; 12 } 13 }
引入前缀和的概念就很容易做了
标签:检索,right,nums,int,303,prefixSum,public,leetcode,left 来源: https://www.cnblogs.com/miracle-zwx/p/15963562.html