当前位置:网站首页>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;
}
};
边栏推荐
- C # create self host webservice
- 深度解析指针与数组笔试题
- EDCircles: A real-time circle detector with a false detection control 翻译
- Software design principles
- 【RISC-V】外部中断
- The next industry outlet: NFT digital collection, is it an opportunity or a foam?
- Self made CA certificate and SSL certificate using OpenSSL
- 暑期刷题-Day3
- Cross origin cross domain request
- [risc-v] external interrupt
猜你喜欢

华为、H3C、思科命令对比,思维导图形式从基础、交换、路由三大方向介绍【转自微信公众号网络技术联盟站】

深入刨析的指针(题解)

Overview of OCR character recognition methods

Performance test method of bank core business system
How to do function test well

Recommended foreign websites for programmers to learn

深入探究指针及指针类型
![[pointer training - eight questions]](/img/fd/1aa3937548a04078c4d7e08198c3a8.png)
[pointer training - eight questions]

canvas切积木小游戏代码
![BUUCTF刷题笔记——[极客大挑战 2019]EasySQL 1](/img/37/c38a933ce7fa5d2b8fa597965ffcb2.png)
BUUCTF刷题笔记——[极客大挑战 2019]EasySQL 1
随机推荐
Sign SSL certificate as Ca
Era5 reanalysis data download strategy
Distributed service framework dobbo
How to choose PLC and MCU?
ERA5再分析资料下载攻略
【指针训练——八道题】
MADDPG的pythorch实现——(1)OpenAI MADDPG环境配置
Idea push rejected solution
[unity3d] GUI control
Computer graduation project asp Net fitness management system VS development SQLSERVER database web structure c programming computer web page source code project
BUUCTF刷题笔记——[极客大挑战 2019]EasySQL 1
指针笔试题~走近大厂
Restful style
jsscript
出现Permission denied的解决办法(750权限谨慎使用)
Problems encountered in 2022 work IV
Linear programming matlab
Exness foreign exchange: the governor of the Bank of Canada said that the interest rate hike would be more moderate, and the United States and Canada fell slightly to maintain range volatility
Jenkins basic knowledge ----- detailed explanation of 03pipeline code
Yyds dry inventory what is test driven development