当前位置:网站首页>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;
}
};
边栏推荐
- StrError & PERROR use yyds dry inventory
- Four logs of MySQL server layer
- ArabellaCPC 2019(补题)
- Computer graduation project asp Net fitness management system VS development SQLSERVER database web structure c programming computer web page source code project
- 继承day01
- Leetcode problem solving -- 173 Binary search tree iterator
- ERA5再分析资料下载攻略
- Tidb ecological tools (backup, migration, import / export) collation
- Leetcode problem solving -- 98 Validate binary search tree
- 【Rust 笔记】18-宏
猜你喜欢

Codeworks 5 questions per day (1700 average) - day 6

ESBuild & SWC浅谈: 新一代构建工具

Image super-resolution using deep convolutional networks(SRCNN)解读与实现

Problems encountered in 2022 work IV

Remote Sensing Image Super-resolution and Object Detection: Benchmark and State of the Art

教你用Pytorch搭建一个自己的简单的BP神经网络( 以iris数据集为例 )

js凡客banner轮播图js特效

深入探究指针及指针类型

2022工作中遇到的问题四

JS regular filtering and adding image prefixes in rich text
随机推荐
StrError & PERROR use yyds dry inventory
Quartz misfire missed and compensated execution
. Net 6 and Net core learning notes: Important issues of net core
Selenium share
SD卡报错“error -110 whilst initialising SD card
Daily question brushing plan-2-13 fingertip life
Résumé des méthodes de reconnaissance des caractères ocr
出现Permission denied的解决办法(750权限谨慎使用)
3857墨卡托坐标系转换为4326 (WGS84)经纬度坐标
MySQL Server层四个日志
深入刨析的指针(题解)
电机控制反Park变换和反Clarke变换公式推导
Performance test method of bank core business system
Redo file corruption repair
Detailed use of dbutils # yyds dry goods inventory #
svg拖动点裁剪图片js特效
Yyds dry inventory what is test driven development
SAP ALV颜色代码对应颜色(整理)
[pointer training - eight questions]
Crazy, thousands of netizens are exploding the company's salary