当前位置:网站首页>暑期刷题-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;
}
};
边栏推荐
- #PAT#day10
- 【若依(ruoyi)】设置主题样式
- . Net 6 and Net core learning notes: Important issues of net core
- Idea push rejected solution
- An article about liquid template engine
- js 正则过滤和增加富文本中图片前缀
- SD card reports an error "error -110 whilst initializing SD card
- [Yu Yue education] basic reference materials of digital electronic technology of Xi'an University of Technology
- 这些不太会
- These are not very good
猜你喜欢
JS regular filtering and adding image prefixes in rich text
[Chongqing Guangdong education] higher mathematics I reference materials of Southwest Petroleum University
Web security SQL injection vulnerability (1)
What is the investment value of iFLYTEK, which does not make money?
深入探究指针及指针类型
下一个行业风口:NFT 数字藏品,是机遇还是泡沫?
JS音乐在线播放插件vsPlayAudio.js
[ruoyi] set theme style
建模规范:命名规范
Is there a completely independent localization database technology
随机推荐
Computer graduation project asp Net fitness management system VS development SQLSERVER database web structure c programming computer web page source code project
Modeling specifications: naming conventions
js 正则过滤和增加富文本中图片前缀
不赚钱的科大讯飞,投资价值该怎么看?
codeforces每日5題(均1700)-第六天
Polymorphic day02
Crazy, thousands of netizens are exploding the company's salary
MySQL advanced notes
The real machine cannot access the shooting range of the virtual machine, and the real machine cannot Ping the virtual machine
蓝色样式商城网站页脚代码
Descriptor implements ORM model
Installation and use tutorial of cobaltstrike-4.4-k8 modified version
An article about liquid template engine
Add one to non negative integers in the array
. Net 6 and Net core learning notes: Important issues of net core
#PAT#day10
[padding] an error is reported in the prediction after loading the model weight attributeerror: 'model' object has no attribute '_ place‘
Single instance mode of encapsulating PDO with PHP in spare time
Who is the winner of PTA
How to do function test well