当前位置:网站首页>剑指offer专项突击版 --- 第 4 天
剑指offer专项突击版 --- 第 4 天
2022-07-31 05:09:00 【米兰的小红黑】

class Solution {
public int subarraySum(int[] nums, int k) {
Map<Integer,Integer> map = new HashMap<>();
map.put(0, 1);
int sum = 0;
int count = 0;
for (int i=0;i<nums.length;++i) {
sum += nums[i];
count += map.getOrDefault(sum-k, 0);
map.put(sum, map.getOrDefault(sum, 0) + 1);
}
return count;
}
}

class Solution {
public int findMaxLength(int[] nums) {
Map<Integer,Integer> map = new HashMap<>();
map.put(0,-1);
int sum = 0;
int ret = 0;
for(int i = 0; i < nums.length; i++){
sum += nums[i] == 0 ? -1 : 1;
if(map.containsKey(sum)){
ret = Math.max(ret, i - map.get(sum));
}else{
map.put(sum,i);
}
}
return ret;
}
}

class Solution {
public int pivotIndex(int[] nums) {
for(int i =0; i < nums.length; i++){
int num = 0;
for(int j = 0; j < nums.length; j++){
if(j < i){
num += nums[j];
}else if(j == i){
continue;
}else{
num -= nums[j];
}
}
if(num == 0){
return i;
}
}
return -1;
}
}

class NumMatrix {
int[][] sums;
public NumMatrix(int[][] matrix) {
int m = matrix.length;
if (m > 0) {
int n = matrix[0].length;
sums = new int[m + 1][n + 1];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
sums[i + 1][j + 1] = sums[i][j + 1] + sums[i + 1][j] - sums[i][j] + matrix[i][j];
}
}
}
}
public int sumRegion(int row1, int col1, int row2, int col2) {
return sums[row2 + 1][col2 + 1] - sums[row1][col2 + 1] - sums[row2 + 1][col1] + sums[row1][col1];
}
}
/**
* Your NumMatrix object will be instantiated and called as such:
* NumMatrix obj = new NumMatrix(matrix);
* int param_1 = obj.sumRegion(row1,col1,row2,col2);
*/
边栏推荐
猜你喜欢

Centos7 install mysql5.7

Typec手机有线网卡网线转网口转接口快充方案

信息系统项目管理师核心考点(五十五)配置管理员(CMO)的工作

面试Redis 高可靠性|主从模式、哨兵模式、Cluster集群模式

有了MVC,为什么还要DDD?

Refinement of the four major collection frameworks: Summary of List core knowledge

MySQL forgot password

MySQL事务(transaction) (有这篇就足够了..)

DVWA shooting range environment construction

MySQL_关于JSON数据的查询
随机推荐
[mysql improves query efficiency] Mysql database query is slow to solve the problem
Centos7 install mysql5.7
Minio上传文件ssl证书不受信任
有了MVC,为什么还要DDD?
Three oj questions on leetcode
DVWA shooting range environment construction
C语言如何分辨大小端
数据集划分以及交叉验证法
12 reasons for MySQL slow query
【JS面试题】面试官:“[1,2,3].map(parseInt)“ 输出结果是什么?答上来就算你通过面试
wx.miniProgram.navigateTo在web-view中跳回小程序并传参
信息系统项目管理师核心考点(五十五)配置管理员(CMO)的工作
ABC D - Distinct Trio (Number of k-tuples
MySQL forgot password
1. Get data - requests.get()
DVWA靶场环境搭建
tf.keras.utils.pad_sequences()
CentOS7 install MySQL graphic detailed tutorial
centos7安装mysql5.7
面试Redis 高可靠性|主从模式、哨兵模式、Cluster集群模式