当前位置:网站首页>剑指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);
*/
边栏推荐
- Unity Framework Design Series: How Unity Designs Network Frameworks
- CentOS7 - yum install mysql
- MySQL事务(transaction) (有这篇就足够了..)
- Pytorch教程Introduction中的神经网络实现示例
- [mysql improves query efficiency] Mysql database query is slow to solve the problem
- DVWA靶场环境搭建
- 可点击也可直接复制指定内容js
- mysql stored procedure
- Temporal介绍
- 再见了繁琐的Excel,掌握数据分析处理技术就靠它了
猜你喜欢

docker安装postgresSQL和设置自定义数据目录

Mysql应用安装后找不到my.ini文件

Centos7 install mysql5.7 steps (graphical version)

如何将项目部署到服务器上(全套教程)

sql语句-如何以一个表中的数据为条件据查询另一个表中的数据

面试官,不要再问我三次握手和四次挥手

Mysql application cannot find my.ini file after installation
【一起学Rust】Rust的Hello Rust详细解析

Summary of MySQL common interview questions (recommended collection!!!)

面试官竟然问我怎么分库分表?幸亏我总结了一套八股文
随机推荐
Temporal线上部署
【一起学Rust】Rust的Hello Rust详细解析
Flask 的初识
CentOS7 —— yum安装mysql
.NET-6.WinForm2.NanUI learning and summary
Summary of MySQL common interview questions (recommended collection!!!)
Mysql应用安装后找不到my.ini文件
pytorch中的一维、二维、三维卷积操作
[Introduction to MySQL 8 to Mastery] Basics - silent installation of MySQL on Linux system, cross-version upgrade
Unity mobile game performance optimization series: performance tuning for the CPU side
With MVC, why DDD?
Distributed Transactions - Introduction to Distributed Transactions, Distributed Transaction Framework Seata (AT Mode, Tcc Mode, Tcc Vs AT), Distributed Transactions - MQ
关于LocalDateTime的全局返回时间带“T“的时间格式处理
wx.miniProgram.navigateTo在web-view中跳回小程序并传参
Flink sink redis 写入Redis
Multiple table query of sql statement
CentOS7 安装MySQL 图文详细教程
MySQL事务(transaction) (有这篇就足够了..)
DVWA installation tutorial (understand what you don't understand · in detail)
pycharm专业版使用