当前位置:网站首页>剑指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);
*/
边栏推荐
猜你喜欢
Kubernetes 证书可用年限修改
Redis进阶 - 缓存问题:一致性、穿击、穿透、雪崩、污染等.
CentOS7 —— yum安装mysql
Mysql——字符串函数
Tapdata 与 Apache Doris 完成兼容性互认证,共建新一代数据架构
SQL row-column conversion
Interviewer, don't ask me to shake hands three times and wave four times again
【LeetCode-SQL每日一练】——2. 第二高的薪水
Distributed transaction processing solution big PK!
Centos7 install mysql5.7
随机推荐
Linux的mysql报ERROR 1045 (28000) Access denied for user ‘root‘@‘localhost‘ (using password NOYSE)
面试官问我TCP三次握手和四次挥手,我真的是
ES source code API call link source code analysis
MySQL事务隔离级别详解
Kubernetes加入集群的TOKEN值过期
Sql解析转换之JSqlParse完整介绍
[Detailed explanation of ORACLE Explain]
tf.keras.utils.get_file()
mysql存储过程
Kubernetes 证书可用年限修改
MySQL8--Windows下使用压缩包安装的方法
pycharm专业版使用
Flink sink redis 写入Redis
110 MySQL interview questions and answers (continuously updated)
MySQL优化之慢日志查询
matlab simulink欠驱动水面船舶航迹自抗扰控制研究
【一起学Rust】Rust学习前准备——注释和格式化输出
sql语句-如何以一个表中的数据为条件据查询另一个表中的数据
账号或密码多次输入错误,进行账号封禁
tf.keras.utils.pad_sequences()