当前位置:网站首页>Brush questions in summer -day3
Brush questions in summer -day3
2022-07-06 03:21:00 【Small grey QAQ】
1.303. Area and retrieval - The array is immutable
class NumArray {
public:
vector<int> sums;
NumArray(vector<int>& nums) {
int n = nums.size();
sums.resize(n + 1);
for (int i = 0; i < n; i++) {
sums[i + 1] = sums[i] + nums[i];
}
}
int sumRange(int i, int j) {
return sums[j + 1] - sums[i];
}
};
2.1588. The sum of all odd length subarrays
class Solution {
public:
int sumOddLengthSubarrays(vector<int>& arr) {
int sum = 0;
int n = arr.size();
for (int start = 0; start < n; start++) {
for (int length = 1; start + length <= n; length += 2) {
int end = start + length - 1;
for (int i = start; i <= end; i++) {
sum += arr[i];
}
}
}
return sum;
}
};
3.930. And the same binary subarray
class Solution {
public:
int numSubarraysWithSum(vector<int>& nums, int goal) {
int sum = 0;
unordered_map<int, int> cnt;
int ret = 0;
for (auto& num : nums) {
cnt[sum]++;
sum += num;
ret += cnt[sum - goal];
}
return ret;
}
};
边栏推荐
- MySQL Server层四个日志
- Huawei, H3C, Cisco command comparison, mind map form from the basic, switching, routing three directions [transferred from wechat official account network technology alliance station]
- Inherit day01
- ArabellaCPC 2019(补题)
- SD card reports an error "error -110 whilst initializing SD card
- [Li Kou] the second set of the 280 Li Kou weekly match
- NR modulation 1
- SAP ALV颜色代码对应颜色(整理)
- Polymorphic day02
- Derivation of anti Park transform and anti Clarke transform formulas for motor control
猜你喜欢
【指针训练——八道题】
华为、H3C、思科命令对比,思维导图形式从基础、交换、路由三大方向介绍【转自微信公众号网络技术联盟站】
【RISC-V】外部中断
Codeforces 5 questions par jour (1700 chacune) - jour 6
EDCircles: A real-time circle detector with a false detection control 翻译
施努卡:3d视觉检测应用行业 机器视觉3d检测
Derivation of anti Park transform and anti Clarke transform formulas for motor control
My C language learning record (blue bridge) -- on the pointer
Microsoft Research, UIUC & Google research | antagonistic training actor critic based on offline training reinforcement learning
Research on cooperative control of industrial robots
随机推荐
Résumé des méthodes de reconnaissance des caractères ocr
电机控制反Park变换和反Clarke变换公式推导
3857墨卡托坐标系转换为4326 (WGS84)经纬度坐标
【Rust 笔记】18-宏
XSS challenges bypass the protection strategy for XSS injection
codeforces每日5題(均1700)-第六天
1003 emergency (25 points), "DIJ deformation"
真机无法访问虚拟机的靶场,真机无法ping通虚拟机
【SLAM】ORB-SLAM3解析——跟踪Track()(3)
How to write compile scripts compatible with arm and x86 (Makefile, cmakelists.txt, shell script)
MySQL Server层四个日志
Restful style
深入探究指针及指针类型
MySQL advanced notes
MPLS experiment
[padding] an error is reported in the prediction after loading the model weight attributeerror: 'model' object has no attribute '_ place‘
Computer graduation project asp Net fitness management system VS development SQLSERVER database web structure c programming computer web page source code project
深度解析指针与数组笔试题
Linear regression and logistic regression
February 14, 2022 Daily: Google long article summarizes the experience of building four generations of TPU