当前位置:网站首页>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;
}
};
边栏推荐
- C # create self host webservice
- 遥感图像超分辨重建综述
- MADDPG的pythorch实现——(1)OpenAI MADDPG环境配置
- XSS challenges bypass the protection strategy for XSS injection
- SD卡报错“error -110 whilst initialising SD card
- 蓝色样式商城网站页脚代码
- JS音乐在线播放插件vsPlayAudio.js
- Detailed use of dbutils # yyds dry goods inventory #
- [concept] Web basic concept cognition
- My C language learning record (blue bridge) -- under the pointer
猜你喜欢
随机推荐
Derivation of anti Park transform and anti Clarke transform formulas for motor control
下一个行业风口:NFT 数字藏品,是机遇还是泡沫?
[Li Kou] the second set of the 280 Li Kou weekly match
八道超经典指针面试题(三千字详解)
codeforces每日5題(均1700)-第六天
施努卡:视觉定位系统 视觉定位系统的工作原理
MPLS experiment
如何做好功能测试
C language judgment, ternary operation and switch statement usage
继承day01
Pytorch基础——(2)张量(tensor)的数学运算
Safety science to | travel, you must read a guide
Idea push rejected solution
Mysql database operation
Quartz misfire missed and compensated execution
resulttype和resultmap的区别和应用场景
Audio-AudioRecord Binder通信机制
Daily question brushing plan-2-13 fingertip life
Audio audiorecord binder communication mechanism
BUUCTF刷题笔记——[极客大挑战 2019]EasySQL 1




![[concept] Web basic concept cognition](/img/27/14bcd73ca70d136436a4382a1b4bd1.jpg)



