当前位置:网站首页>暑期刷题-Day3
暑期刷题-Day3
2022-07-06 03:13:00 【小灰QAQ】
1.303. 区域和检索 - 数组不可变
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. 所有奇数长度子数组的和
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. 和相同的二元子数组

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;
}
};
边栏推荐
猜你喜欢

Taobao focus map layout practice

Linear programming matlab

Analyze menu analysis

The next industry outlet: NFT digital collection, is it an opportunity or a foam?

Jenkins basic knowledge ----- detailed explanation of 03pipeline code

XSS challenges绕过防护策略进行 XSS 注入

IPv6 comprehensive experiment

IPv6 jobs

八道超经典指针面试题(三千字详解)

Computer graduation project asp Net fitness management system VS development SQLSERVER database web structure c programming computer web page source code project
随机推荐
有没有完全自主的国产化数据库技术
Idea push rejected solution
canvas切积木小游戏代码
[Chongqing Guangdong education] higher mathematics I reference materials of Southwest Petroleum University
Deep parsing pointer and array written test questions
Safety science to | travel, you must read a guide
[ruoyi] enable Mini navigation bar
IPv6 jobs
【Kubernetes 系列】一文學會Kubernetes Service安全的暴露應用
Jenkins basic knowledge ----- detailed explanation of 03pipeline code
ArabellaCPC 2019(补题)
Item 10: Prefer scoped enums to unscoped enums.
【指针训练——八道题】
codeforces每日5题(均1700)-第六天
I sorted out a classic interview question for my job hopping friends
My C language learning records (blue bridge) -- files and file input and output
[padding] an error is reported in the prediction after loading the model weight attributeerror: 'model' object has no attribute '_ place‘
蓝色样式商城网站页脚代码
Mysql database operation
【Kubernetes 系列】一文学会Kubernetes Service安全的暴露应用