当前位置:网站首页>The first day of June training - array
The first day of June training - array
2022-06-12 06:29:00 【Straying into the crowd】
List of articles
- Preface
- One 、[ The sum of all odd length subarrays ](https://leetcode.cn/problems/sum-of-all-odd-length-subarrays/)
- Two 、[ Minimum distance to target element ](https://leetcode.cn/problems/minimum-distance-to-the-target-element/)
- 3、 ... and 、[ Dismantle the bomb ](https://leetcode.cn/problems/defuse-the-bomb/)
Preface
The content of today's algorithm is : Array
One 、 The sum of all odd length subarrays
Here's an array of positive integers arr , Please calculate the sum of all possible odd length subarrays .
Subarray Defined as a continuous subsequence in the original array .
Please return arr in The sum of all odd length subarrays
One 、 Ideas :
Traversal and accumulation of odd length arrays
Two 、 Source code
class Solution {
public:
int sumOddLengthSubarrays(vector<int>& arr) {
// Two pointers A total number
int i,j,sum=0;
for(i=0 ;i<arr.size() ;i++){
// Local count Variable
int count=0;
// Local sum
int s=0;
for(j=i ;j<arr.size() ;j++){
// Count
++count;
// Add up
s+=arr[j];// error found ————> Error for ————> The subscript is wrong —— Such as ——>s+=arr[i];——————————> terms of settlement ————————> Be careful next time
// take Count —————— by ————————> An array of odd lengths ;
if(count&1){
sum+=s;
}
}
}
return sum;
}
};
3、 ... and . Knowledge point
enumeration
Two 、 Minimum distance to target element
Give you an array of integers nums ( Subscript from 0 Start Count ) And two integers target and start , Please find a subscript i , Satisfy nums[i] == target And abs(i - start) To minimize the . Be careful :abs(x) Express x The absolute value of .
return abs(i - start) .
Topic data assurance target Exist in nums in .
One 、 Ideas :
Simulate directly according to the question
Two 、 Source code
class Solution {
public:
int getMinDistance(vector<int>& nums, int target, int start) {
// Loop variable Minimum distance variable
int i,min=1000000;
for(i=0 ;i<nums.size() ;i++){
// If the current value be equal to The target And the current subscript subtract Given value Is the minimum Just assign
if( nums[i] == target && abs(i - start) < min){
min = abs(i - start);
}
}
return min; //return 0; Return variable The variable is written as 0 了 ...pdf
}
};
3、 ... and . Knowledge point
enumeration
3、 ... and 、 Dismantle the bomb
You have a bomb to dismantle , Pressed for time ! Your agent will give you a length of n Of loop Array code And a key k .
To get the right password , You need to replace every number . All the numbers will meanwhile Be replaced .
If k > 0 , Will be the first i For numbers Next k The sum of the numbers replaces .
If k < 0 , Will be the first i For numbers Before k The sum of the numbers replaces .
If k == 0 , Will be the first i For numbers 0 Replace .
because code It's cyclical , code[n-1] The next element is code[0] , And code[0] The first element is code[n-1] .
Here you are. loop Array code And integer keys k , Please return the decrypted results to dismantle the bomb !
One 、 Ideas :
Simulate by question , Module a range , Take value before or after counting
Two 、 Source code
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;
// It can be executed
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;
// There's a problem ,// The problem j And negative End directly
for(j=0 ;j<abs(k) ;j++){
printf("%d\n",k);
--copy;
val+=code[(copy+len)%len];// The problem ——>copy+len-1————>val+=code[(copy+len-1)%len];———— Ben would have walked straight from the back -1 Will take one more step
}
}
// It can be executed
if(k==0){
val=0;
}
ret.push_back(val);
}
return ret;
}
};
// What I learned :
// Yes vector Application ( At first, I didn't quite understand Look at other code to know 了 operation )2. Front and rear Traverse trend Go back and forth
3、 ... and . Knowledge point
1. Yes vector Application ( At first, I didn't quite understand Look at other code to know 了 operation ) The number to be put in Operation comes out ,ret.push_back( Target number ) that will do
2. Front and rear Traverse trend Go back and forth ( modulus )
边栏推荐
- Process when solving vagrant up_ builder. rb:43:in `join‘: incompatible character encodings: GBK and UTF-8
- PHP 开发环境搭建及数据库增删改查
- Unity custom translucent surface material shader
- 六月集训 第一日——数组
- LeetCode-1741. Find total time spent per employee
- Multithreading (V) -- Concurrent tools (II) -- j.u.c concurrent contracting (I) -- AQS and reentrantlock principles
- An error occurred while downloading the remote file The errormessage
- leetcode 704. Binary search
- SQL注入——联合查询union
- Book classification based on Naive Bayes
猜你喜欢

Tips for using the potplayer video player

Codeforces Round #793 (Div. 2) A B C

Textcnn (MR dataset - emotion classification)

Modifying theme styles in typora

基于报错的 SQL 注入

SQL injection based on error reporting

Bulk Rename Utility

Multithreading mode (I) -- protective pause and join source code

Summary of some problems in sensor bring up

. Net core - pass Net core will Net to cross platform
随机推荐
leetcode 35. Search insert location
RNN model
Solution: content type 'application/x-www-form-urlencoded; charset=UTF-8‘ not supported
六月集训 第九日——位运算
Unity C script implements AES encryption and decryption
LeetCode个人题解(剑指offer3-5)3.数组中重复的数字,4.二维数组中的查找,5.替换空格
About why GPU early-z reduces overdraw
Deep and detailed analysis of PHP one sentence Trojan horse
LeetCode-1716. Calculate the amount deducted from the bank
leetcode 278. First wrong version
Multithreading (V) -- concurrency tools (I) -- thread pool (II) -- related contents of ThreadPoolExecutor
Trunet: short videos generation from long videos via story preserving truncation (thesis translation)
Automatic modeling of Interchange
Dlib face detection
Unity can realize the rotation, translation and scaling script of the camera around the target point on the mobile terminal device
六月集训 第七日 ——哈希表
Solution: unsatisfieddependencyexception: error creating bean with name 'authaspect':
LeetCode-1873. Calculate special bonus
In unity3d, billboard effect can be realized towards another target
n次贝塞尔曲线