当前位置:网站首页>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;
}
};
边栏推荐
- How to do function test well
- Reverse repackaging of wechat applet
- jsscript
- 多态day02
- 遥感图像超分辨率论文推荐
- Princeton University, Peking University & UIUC | offline reinforcement learning with realizability and single strategy concentration
- [unity3d] GUI control
- Distributed service framework dobbo
- 施努卡:视觉定位系统 视觉定位系统的工作原理
- [pointer training - eight questions]
猜你喜欢
EDCircles: A real-time circle detector with a false detection control 翻译
Safety science to | travel, you must read a guide
C language judgment, ternary operation and switch statement usage
SAP ALV单元格级别设置颜色
NR modulation 1
ESBuild & SWC浅谈: 新一代构建工具
适合程序员学习的国外网站推荐
[risc-v] external interrupt
如何做好功能测试
记录一下逆向任务管理器的过程
随机推荐
MySQL Server层四个日志
The next industry outlet: NFT digital collection, is it an opportunity or a foam?
IPv6 jobs
Performance analysis of user login TPS low and CPU full
Buuctf question brushing notes - [geek challenge 2019] easysql 1
给新人工程师组员的建议
February 14, 2022 Daily: Google long article summarizes the experience of building four generations of TPU
Leetcode problem solving -- 98 Validate binary search tree
three.js网页背景动画液态js特效
An article about liquid template engine
Descriptor implements ORM model
My C language learning record (blue bridge) -- under the pointer
银行核心业务系统性能测试方法
Era5 reanalysis data download strategy
These are not very good
Microsoft Research, UIUC & Google research | antagonistic training actor critic based on offline training reinforcement learning
Daily question brushing plan-2-13 fingertip life
适合程序员学习的国外网站推荐
Mysqldump data backup
Software design principles