当前位置:网站首页>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 )
边栏推荐
- 夜神模擬器adb查看log
- LeetCode-1587. Bank account summary II
- Computer composition and design work05 ——fifth verson
- Solution: unsatisfieddependencyexception: error creating bean with name 'authaspect':
- 基于报错的 SQL 注入
- Excel VBA opens a file that begins with the specified character
- LeetCode-1189. Maximum number of balloons
- LeetCode-1350. Invalid students
- SQL注入原理即sqli-labs搭建,sql注入简单实战
- Cv2.fillpoly coco annotator segment coordinate conversion to mask image
猜你喜欢

Word2Vec

Redis basic notes

leetcode 300. Longest increasing subsequence

Redis configuration (IV) -- cluster

Multithreading (V) -- concurrency tools (I) -- thread pool (II) -- related contents of ThreadPoolExecutor

Deep and detailed analysis of PHP one sentence Trojan horse

Trunet: short videos generation from long videos via story preserving truncation (thesis translation)

Leetcode January 10 daily question 306 Additive number

Cv2.fillpoly coco annotator segment coordinate conversion to mask image

Word vector training based on nnlm
随机推荐
Multithreading (IV) -- no lock (IV) -- unsafe
MLP sensor
Redis problem (I) -- cache penetration, breakdown, avalanche
Qt-- realize TCP communication
LeetCode-884. Unusual words in two sentences
[easyexcel] easyexcel checks whether the header matches the tool class encapsulated in easyexcel, including the field verification function. You can use validate to verify
Introduction to the method of diligently searching for the alliance procedure
LeetCode-1185. Day of the week
Chartextcnn (Ag dataset - news topic classification)
Remap function of C different interval mapping
Leetcode January 10 daily question 306 Additive number
Automatic modeling of Interchange
Use ms17-010 Eternal Blue vulnerability to infiltrate win7 and establish a permanent back door
SQL injection - blind injection
Redis application (I) -- distributed lock
torch在高版本训练的模型在低版本中使用报错
JS pre parsing
The difference between get and post and the code implementation of message board
The principle of SQL injection is to build sqli labs, and SQL injection is simple and practical
Pytorch implementation of regression model