前言
记录从数组开始的技巧,目地是将刷过的题进行归纳总结,同时给有需要的小伙伴们一些参考
前缀和
前缀和用来计算某一个区间的和的大小,也就是代表一维,二维数组我们可以计算任意区间的大小
560和为K的子数组
思路
- 首先题目说了和为K,然后统计连续的子数组,连续的子数组就代表你的数组索引是连续的
- 而我们前缀和是计算一个数组【i,j]的和
- 看下图,我要计算索引区间【1-4】的元素和,就可以通过preSum[5]-preSum[1]得出
class Solution {
public int subarraySum(int[] nums, int k) {
int n = nums.length;
int[]preSum = new int[n+1];
for(int i = 1;i<preSum.length;i++){
preSum[i] = preSum[i-1] + nums[i-1];
}
int res = 0;
for(int i = 1;i<=n;i++){
for(int j = 0;j<i;j++){
if(preSum[i]-preSum[j]==k){
res++;
}
}
}
return res;
}
}
int [] count = new int[100+1];
for(int score:scores){
count[score]++;
}
304、二维区域和检索-矩阵不可变
如果我想计算红?的这个?矩阵的元素之和,可以?绿?矩阵减去蓝?矩阵减去橙?矩阵最后加上粉?矩 阵,?绿蓝橙粉这四个矩阵有?个共同的特点,就是左上?就是 (0, 0) 原点
- 同时我们发现当我们二维数组计算的时候,presum左上角的被减去了两次
class NumMatrix {
private int[][]presum;
public NumMatrix(int[][] matrix) {
int m = matrix.length;
int n = matrix[0].length;
presum = new int[m+1][n+1];
for(int i = 1;i<=m;i++){
for(int j = 1;j<=n;j++){
presum[i][j] = presum[i-1][j]+presum[i][j-1]+matrix[i-1][j-1]-presum[i-1][j-1];
}
}
}
public int sumRegion(int row1, int col1, int row2, int col2) {
return presum[row2+1][col2+1] - presum[row1][col2+1]-presum[row2+1][col1]+presum[row1][col1];
}
}
差分数组
差分数组主要适用于我们的数组在不修改的情况下进行的原数组的增减操作
int[]diff = new int[nums.length];
//构造差分数组
for(int i =1;i<nums.length;i++){
diff[i] = nums[i] - nums[i-1];
}
- 根据diff差分数组是可以反推出原始数组nums的,代码如下
int[]res = new int[diff.length];
res[0] = diff[0];
for(int i = 1;i<diff.length;i++){
res[i] = res[i-1]+diff[i];
}
class Difference{
private int[] diff;
public Difference (int[]diff){
assert nums.length > 0;
diff = new int[nums.length];
diff[0] = nums[0];
for(int i =1;i<nums.length;i++){
diff[i] = nums[i] - nums[i-1];
}
}
public void increment(int i ,int j,int val){
diff[i]+=val;
for(j+1<nums.length){
diff[j+1]-=val;
}
}
public int[]result(){
int[] res = new int[diff.length];
res[0] = diff[0];
for(int i =1;i<diff.length;i++){
res[i] = res[i-1]+diff[i];
}
return res;
}
}
370区间加法
class solution{
class Difference{
private int[] diff;
public Difference (int[]diff){
assert nums.length > 0;
diff = new int[nums.length];
diff[0] = nums[0];
for(int i =1;i<nums.length;i++){
diff[i] = nums[i] - nums[i-1];
}
}
public void increment(int i ,int j,int val){
diff[i]+=val;
for(j+1<nums.length){
diff[j+1]-=val;
}
}
public int[]result(){
int[] res = new int[diff.length];
res[0] = diff[0];
for(int i =1;i<diff.length;i++){
res[i] = res[i-1]+diff[i];
}
return res;
}
}
int[]getModifiedArray(int length,int[][]updates){
int[]nums = new int[length];
Difference df = new Difference(nums);
for(int[]update : updates){
int i = update[0];
int j = update[1];
int val = update[2];
df.increment(i,j,val);
}
return df.result;
}
}
class Solution {
public int[] corpFlightBookings(int[][] bookings, int n) {
int[]nums = new int[n];
Difference df = new Difference(nums);
for(int[]book : bookings){
int i = book[0]-1;
int j = book[1]-1;
int val = book[2];
df.increment(i,j,val);
}
return df.result();
}
class Difference{
private int[] diff;
public Difference(int[]nums){
assert nums.length>0;
diff = new int[nums.length];
diff[0] = nums[0];
for(int i = 1;i < nums.length;i++){
diff[i] = nums[i]-nums[i-1];
}
}
public void increment(int i,int j,int val){
diff[i]+=val;
if(j+1<diff.length){
diff[j+1]-=val;
}
}
public int[]result(){
int[]res = new int[diff.length];
res[0] = diff[0];
for(int i = 1;i<diff.length;i++){
res[i] = res[i-1] + diff[i];
}
return res;
}
}
}
|