当前位置:网站首页>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;
}
};
边栏推荐
猜你喜欢
随机推荐
Analyze 菜单分析
Redis cache breakdown, cache penetration, cache avalanche
Research on cooperative control of industrial robots
XSS challenges bypass the protection strategy for XSS injection
Web security SQL injection vulnerability (1)
ASU & OSU | model based regularized off-line meta reinforcement learning
[padding] an error is reported in the prediction after loading the model weight attributeerror: 'model' object has no attribute '_ place‘
手写数据库客户端
Image super-resolution using deep convolutional networks(SRCNN)解读与实现
ArabellaCPC 2019(补题)
The real machine cannot access the shooting range of the virtual machine, and the real machine cannot Ping the virtual machine
three.js网页背景动画液态js特效
Differences and application scenarios between resulttype and resultmap
【指针训练——八道题】
SD卡報錯“error -110 whilst initialising SD card
. Net 6 and Net core learning notes: Important issues of net core
JS regular filtering and adding image prefixes in rich text
Advanced learning of MySQL -- Fundamentals -- isolation level of transactions
MySQL advanced notes
Remote Sensing Image Super-resolution and Object Detection: Benchmark and State of the Art