当前位置:网站首页>六月集训 第一日——数组
六月集训 第一日——数组
2022-06-12 06:27:00 【误入佬群】
文章目录
前言
今天算法的内容是:数组
一、所有奇数长度子数组的和
给你一个正整数数组 arr ,请你计算所有可能的奇数长度子数组的和。
子数组 定义为原数组中的一个连续子序列。
请你返回 arr 中 所有奇数长度子数组的和
一、思路:
遍历累加奇数长度数组
二、源码
class Solution {
public:
int sumOddLengthSubarrays(vector<int>& arr) {
//两个指针 一个总数
int i,j,sum=0;
for(i=0 ;i<arr.size() ;i++){
//局部计数 变量
int count=0;
//局部和
int s=0;
for(j=i ;j<arr.size() ;j++){
//进行计数
++count;
//进行累加
s+=arr[j];//找到错误————>错误为————>下标写错了——如——>s+=arr[i];——————————>解决办法————————>下次要细心
//取 计数——————为————————> 奇数长度的数组;
if(count&1){
sum+=s;
}
}
}
return sum;
}
};
三.知识点
枚举
二、 到目标元素的最小距离
给你一个整数数组 nums (下标 从 0 开始 计数)以及两个整数 target 和 start ,请你找出一个下标 i ,满足 nums[i] == target 且 abs(i - start) 最小化 。注意:abs(x) 表示 x 的绝对值。
返回 abs(i - start) 。
题目数据保证 target 存在于 nums 中。
一、思路:
直接按题模拟
二、源码
class Solution {
public:
int getMinDistance(vector<int>& nums, int target, int start) {
//循环变量 最小距离变量
int i,min=1000000;
for(i=0 ;i<nums.size() ;i++){
// 若当前值 等于 目标值 且当前下标 减去 给值 为最小值 就赋值
if( nums[i] == target && abs(i - start) < min){
min = abs(i - start);
}
}
return min; //return 0;返回变量 变量写成 0了...pdf
}
};
三.知识点
枚举
三、拆炸弹
你有一个炸弹需要拆除,时间紧迫!你的情报员会给你一个长度为 n 的 循环 数组 code 以及一个密钥 k 。
为了获得正确的密码,你需要替换掉每一个数字。所有数字会 同时 被替换。
如果 k > 0 ,将第 i 个数字用 接下来 k 个数字之和替换。
如果 k < 0 ,将第 i 个数字用 之前 k 个数字之和替换。
如果 k == 0 ,将第 i 个数字用 0 替换。
由于 code 是循环的, code[n-1] 下一个元素是 code[0] ,且 code[0] 前一个元素是 code[n-1] 。
给你 循环 数组 code 和整数密钥 k ,请你返回解密后的结果来拆除炸弹!
一、思路:
按题模拟,模一个范围,进行计数前或后取值
二、源码
class Solution {
public:
vector<int> decrypt(vector<int>& code, int k) {
int i,j,copy,val;
int len=code.size();
vector<int>ret;
for(i=0 ;i<len ;i++){
val=0;
//可以执行
if(k>0){
copy=i;
for(j=0 ;j<k ;j++){
++copy;
val+=code[copy%len];
}
}
if(k<0){
// printf("%d\n",k);
copy=i;
//这有问题,//问题所在 j 与负数 直接结束
for(j=0 ;j<abs(k) ;j++){
printf("%d\n",k);
--copy;
val+=code[(copy+len)%len];//问题所在——>copy+len-1————>val+=code[(copy+len-1)%len];————本就会直接从后往前走的-1会多走一步
}
}
//可以执行
if(k==0){
val=0;
}
ret.push_back(val);
}
return ret;
}
};
//学到的知识:
//对vector的应用(一开始不是很懂 看了下别的代码知道 了 操作)2.前后的 遍历走向 从后往前遍历
三.知识点
1.对vector的应用(一开始不是很懂 看了下别的代码知道 了 操作)将要进行放入的数 操作出来,ret.push_back(目标数) 即可
2.前后的 遍历走向 从后往前遍历(取模)
边栏推荐
- Multithreading mode (I) -- protective pause and join source code
- Touch screen setting for win7 system dual screen extended display
- RMB classification II
- leetcode 35. Search insert location
- LeetCode-1189. Maximum number of balloons
- Redis data structure (VIII) -- Geo
- JS pre parsing
- C2w model - language model
- Multithreading (V) -- Concurrent tools (II) -- j.u.c concurrent contracting (I) -- AQS and reentrantlock principles
- Zip and Items() difference
猜你喜欢

Overview of camera image quality

Piecewise Bezier curve

Touch screen setting for win7 system dual screen extended display

leetcode 300. Longest increasing subsequence

In unity3d, billboard effect can be realized towards another target

Modifying theme styles in typora

张驰课堂:2022年CAQ中质协六西格玛考试时间通知

SQL 注入-盲注

Using hidden Markov model to mark part of speech

摄像头拍摄运动物体,产生运动模糊/拖影的原因分析
随机推荐
LeetCode-1154. Day of the year
C # converts the hexadecimal code form of text to text (ASCII)
Single channel picture reading
Redis basic notes
leetcode 35. Search insert location
Univariate linear regression model
[reinstall system] 01 system startup USB flash disk production
Apache poi 导入导出Excel文件
PDF. JS help file
N-degree Bessel curve
The unity3d script searches for colliders with overlaps within the specified radius
Remap function of C different interval mapping
Multithreading (2) -- pipeline (2) -- synchronized underlying monitor, lightweight, biased lock, lock inflation
Zip and Items() difference
LeetCode-419. Battleship on deck
Solution: unsatisfieddependencyexception: error creating bean with name 'authaspect':
相机图像质量概述
LeetCode-1350. Invalid students
SQL injection - Union query
Video based fire smoke detection using robust AdaBoost