当前位置:网站首页>剑指offer专项突击版第4天
剑指offer专项突击版第4天
2022-07-20 05:39:00 【hys__handsome】
剑指 Offer II 010. 和为 k 的子数组
前缀和+哈希表+在线操作
class Solution {
public:
int subarraySum(vector<int>& nums, int k) {
int n = nums.size();
unordered_map<int,int> um;
int cnt = 0;
um[0] = 1; //前缀和为0的个数,由于没有元素的话和为0,所以这里个数为1
for(int i = 0,sum = 0; i < n; i++){
sum += nums[i];
if(um.count(sum-k) != 0) cnt += um[sum-k];
um[sum] ++ ;
}
return cnt;
}
};
剑指 Offer II 011. 0 和 1 个数相同的子数组
前缀和+哈希表
- c o u n t e r counter counter 记录当前多出来 1 1 1 的个数。
- 只需要枚举 i i i ,然后哈希表记录每个 i i i 对应的 c o u n t e r counter counter 值。
- 然后计算的时候就找哈希表是否有这个 c o u n t e r counter counter值,如果有返回对应的下标(与当前下标做差即为 1 1 1 和 0 0 0 数量相同的连续子数组长度)。
class Solution {
public:
int findMaxLength(vector<int>& nums) {
int n = nums.size();
unordered_map<int,int> um;
int res = 0, counter = 0;
um[counter] = -1;
for(int i = 0; i < n; i++) {
if(nums[i] == 1) counter++;
else counter--;
if(um.count(counter) != 0 ) {
int preidx = um[counter];
res = max(res,i-preidx);
} else {
um[counter] = i;
}
}
return res;
}
};
剑指 Offer II 012. 左右两边子数组的和相等
前缀和
class Solution {
public:
int pivotIndex(vector<int>& nums) {
int total = 0;
for(auto num : nums) total += num;
for(int i = 0, sum = 0; i < nums.size(); i++) {
if(total - sum - nums[i] == sum) return i;
sum += nums[i];
}
return -1;
}
};
剑指 Offer II 013. 二维子矩阵的和
经典二维前缀和
class NumMatrix {
public:
vector<vector<int>> pre;
NumMatrix(vector<vector<int>>& matrix) {
int n = matrix.size(), m = matrix[0].size();
pre.resize(n+1,vector<int>(m+1));
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++) {
pre[i+1][j+1] += matrix[i][j] + pre[i+1][j] + pre[i][j+1] - pre[i][j];
// cout<<pre[i+1][j+1]<<(j == m-1 ? '\n' : ' ');
}
}
int sumRegion(int row1, int col1, int row2, int col2) {
row1++, col1++,row2++,col2++;
return pre[row2][col2] - pre[row2][col1-1] - pre[row1-1][col2] + pre[row1-1][col1-1];
}
};
边栏推荐
- [model course of the first cann training camp advanced class in 2022] the first big assignment and additional content
- Play with the one-stop plan of cann target detection and recognition [basic]
- 色彩空间(2)—— YUV
- Keras调用plot_model报错解决方案
- 热能动力工程毕业论文题目
- DC-1-实践
- UGUI——ToggleGroup
- HDF5 library version mismatched error的解决方案
- The meaning of coordinates in qlineargradius
- 使用libwebp解码webp静态图片
猜你喜欢
Off chip spiflash embedded in FatFs based on stm32cubemx
STM32CubeMX的Flash读写问题
SourceTree对代码版本管理的操作流程及故障处理(不定时更新)
[Unity脚本优化] Optimizing garbage collection in Unity games
UGUI——RectMask2D
Tools - idea nice style font / size/
Given a positive integer n, it is expressed as the addition of numbers 1,3,7,15. Please code to find the combination that minimizes the total number of occurrences of the above numbers (each number ca
运算放大器的简要理解
Model thesis of radio, film and television program production
香蕉派BPI-M5折腾记录(3)—— 编译BSP
随机推荐
Building space temperature distribution prediction model and temperature curve drawing graduation thesis
JupyterNotebook插件管理与安装
广播影视节目制作毕业论文范文
【STM32F103RCT6】电机PWM驱动模块思路与代码
结合源码看《我所理解的cocos2dx-3.0》—— 概述
[Unity脚本优化]Optimizing scripts in Unity games——2019以下
Markdown中编辑数学公式
FPGA学习准备
什么是渲染管道,怎么绘制3D物体
热能动力工程毕业论文题目
Spark SQL case (I)
kettle优化之提高MySQL读写速度
力扣总结之链表题目
实验四 CTF实践
STM32CubeMonitor的使用第二部分-历史数据存储以及网络访问
学习记录十三
建筑空间温度分布预测模型与温度曲线图绘制毕业论文
Graduation thesis topics of hydrology and water resources [213]
Chapter002-FPGA学习之按键控制LED灯和蜂鸣器
回溯解决组合问题