当前位置:网站首页>六月集训 第一日——数组
六月集训 第一日——数组
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.前后的 遍历走向 从后往前遍历(取模)
边栏推荐
- LeetCode-1576. Replace all question marks
- Unity3d display FPS script
- QT--实现TCP通信
- leetcode 300. Longest increasing subsequence
- Unity implements smooth interpolation
- Single channel picture reading
- PHP development environment construction and database addition, deletion, modification and query
- LeetCode个人题解(剑指offer3-5)3.数组中重复的数字,4.二维数组中的查找,5.替换空格
- Chartextcnn (Ag dataset - news topic classification)
- 基于报错的 SQL 注入
猜你喜欢

基于报错的 SQL 注入

UE4 4.27 modify the mobile forward pipeline to support cluster multi light source culling

Textcnn (MR dataset - emotion classification)

C2w model - language model

Modifying theme styles in typora

Multithreading (V) -- Concurrent tools (II) -- j.u.c concurrent contracting (I) -- AQS and reentrantlock principles

Piecewise Bezier curve

PHP 读写 COOKIE

Bert Chinese classification model training + reasoning + deployment

Qt-- realize TCP communication
随机推荐
The principle of SQL injection is to build sqli labs, and SQL injection is simple and practical
Mastering UI development with unity
Leetcode January 10 daily question 306 Additive number
Redis data type (VII) -- hyperloglog
SQL injection read / write file
Redis queue
集合判断存在交集
Get the size of the picture
Redis problem (I) -- cache penetration, breakdown, avalanche
What states do threads have?
Single channel picture reading
Cause analysis of motion blur / smear caused by camera shooting moving objects
Video based fire smoke detection using robust AdaBoost
Codeforces Round #793 (Div. 2) A B C
LeetCode-1185. Day of the week
AI operation ch8
Redis basic notes
Android studio mobile development creates a new database and obtains picture and text data from the database to display on the listview list
platform driver
分段贝塞尔曲线