当前位置:网站首页>暑期刷题-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;
}
};
边栏推荐
- 4. File modification
- How to do function test well
- Eight super classic pointer interview questions (3000 words in detail)
- Classic interview question [gem pirate]
- Prototype design
- Buuctf question brushing notes - [geek challenge 2019] easysql 1
- MySQL advanced notes
- Item 10: Prefer scoped enums to unscoped enums.
- Is there a completely independent localization database technology
- 1003 emergency (25 points), "DIJ deformation"
猜你喜欢
Tidb ecological tools (backup, migration, import / export) collation
Performance analysis of user login TPS low and CPU full
Safety science to | travel, you must read a guide
JS音乐在线播放插件vsPlayAudio.js
My C language learning record (blue bridge) -- under the pointer
Performance test method of bank core business system
[Chongqing Guangdong education] higher mathematics I reference materials of Southwest Petroleum University
Precautions for single chip microcomputer anti reverse connection circuit
Overview of OCR character recognition methods
银行核心业务系统性能测试方法
随机推荐
Erreur de la carte SD "erreur - 110 whilst initialisation de la carte SD
【Kubernetes 系列】一文學會Kubernetes Service安全的暴露應用
Installation and use tutorial of cobaltstrike-4.4-k8 modified version
canvas切积木小游戏代码
Communication between microservices
MySQL learning notes-10-tablespace recycling
Leetcode problem solving -- 98 Validate binary search tree
I sorted out a classic interview question for my job hopping friends
Codeforces 5 questions par jour (1700 chacune) - jour 6
Add one to non negative integers in the array
Tidb ecological tools (backup, migration, import / export) collation
手写数据库客户端
resulttype和resultmap的区别和应用场景
蓝色样式商城网站页脚代码
【 kubernets series】 a Literature Study on the Safe exposure Applications of kubernets Service
Explore pointers and pointer types in depth
Microsoft Research, UIUC & Google research | antagonistic training actor critic based on offline training reinforcement learning
3857墨卡托坐标系转换为4326 (WGS84)经纬度坐标
three.js网页背景动画液态js特效
My C language learning record (blue bridge) -- on the pointer