当前位置:网站首页>Brush force buckle from 0
Brush force buckle from 0
2022-06-10 09:27:00 【Shadow, are you tired with me?】

Write it at the front :
Blogger Homepage : To poke , Welcome the big guy to give directions !
Blogger Qiu :QQ:1477649017 Welcome like-minded friends to come on
Goal dream : Enter the large factory , Determined to be a cow breaking Java Program the ape , Although he is still a little rookie now, hehe
----------------------------- Thank you for being so handsome and beautiful. Give me some praise ! More than a heart -----------------------------
from 0 Start brush force buckle
One , Array class
1.1, Traversal of array
Question no :485, Maximum continuous 1 The number of

Subject portal , Poke poke began to solve the problem >>
class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int count = 0;
int max = 0;
for(int i = 0;i < nums.length;i++){
if(nums[i] == 1){
count++;
}else{
count = 0;
}
if(count > max){
max = count;
}
}
return max;
}
}
This question examines the traversal of arrays and your ability to observe , Note that this is a binary one-dimensional array , As the tip says , Array element it is not 0 Namely 1, Then we should count the continuous 1 The number of , encounter 1 Just put count++, And synchronously save to max Inside , If the array element is 0 了 , Just put count Zero clearing , And continue to count again , Of course before max Or has been stored encounter 0 The previous continuous 1 The number of , Later, as long as it is updated in real time max Just fine .
Question no :495, Timo attacks

Subject portal , Poke poke began to solve the problem >>
class Solution {
public int findPoisonedDuration(int[] timeSeries, int duration) {
int ret = 0;
int end = 0;// Record the theoretical end time of poisoning
for(int i = 0;i < timeSeries.length;i++){
if(timeSeries[i] >= end){
ret += duration;
}else{
ret += timeSeries[i] + duration - end;
}
end = timeSeries[i] + duration;
}
return ret;
}
}
In fact, this problem should not be too complicated , It is a problem of cumulative calculation of poisoning time , The only thing that needs our attention is to reset the poisoning effect . Every time we receive an attack , The time of poisoning will be duration, So let's not consider the overlapping of our time , First of all duration Add up , Then subtract the overlap from each time , That will finally be our actual poisoning time .
Put this idea on every attack ,end It records the end time of our theoretical poisoning ( Do not consider being attacked in the middle , as well as end This moment is not poisoned ), So our
timeSeries[i] >= end, Then there is no overlap , How many direct attacks , hold duration Just add up , Of course, every time end It will update , When not satisfied timeSeries[i] >= end Under this condition , It means that the class was attacked during the time when the poisoning took effect , So at this time, it is reasonable that our poisoning time can not simply add duration 了 , Because the attack time at this time overlaps with the last poisoning time , This part must be subtracted , What about this overlapping part , Namely timeSeries[i] - end, The result is negative , The effect of adding is to reduce .
Question no :414, The third largest number

Subject portal , Poke poke began to solve the problem >>
class Solution {
public int thirdMax(int[] nums) {
if(nums.length == 1){
return nums[0];
}
if(nums.length == 2){
return (nums[0] > nums[1]) ? nums[0] : nums[1];
}
Arrays.sort(nums);
int ret = 0;
int count = 0;
int tmp = Integer.MAX_VALUE;
for(int i = nums.length - 1 ;i >= 0;i--){
if(nums[i] != tmp){
count++;
}
tmp = nums[i];
if(count == 3){
break;
}
}
if(count == 3){
return tmp;
}else{
return nums[nums.length - 1];// The array has no third largest number , for example 2 2 2 2 2 8;
}
}
}
Two special cases are listed separately , The array has only two numbers , Or the array has only one number , At this time, it is relatively simple to find the third largest number . Excluding these two cases , Sort the array first , So the array is arranged from small to large , Start traversing from the end of the array , As long as there is no quarrel tmp equal , Our count variable count++, Until we find the third largest number . Make use of count The reason for this is that the element has the possibility of repetition , So we sorted it out , The penultimate number is not necessarily the third largest number .
Question no :628, The maximum product of three numbers 
Subject portal , Poke poke began to solve the problem >>
class Solution {
public int maximumProduct(int[] nums) {
// Three pointer traversal is too inefficient
Arrays.sort(nums);
int n = nums.length - 1;
return Math.max(nums[0]*nums[1]*nums[n],nums[n-2]*nums[n-1]*nums[n]);
}
}
Sort the array first , There are three cases of maximum product :
1, The last three numbers of the array are all positive numbers , The maximum product is the product of the last three numbers .
2, The last three numbers of the array are all negative numbers , The maximum product is also the product of the last three numbers . The size comparison of negative numbers is opposite to that of positive numbers
3, There are positive and negative numbers in the last three numbers of the array , It can only be the product of the smallest two negative numbers and the largest positive numbers .
So take it all into consideration , That is to integrate and use Math.max Just find the maximum , It includes the above situation .
Last , Today's article sharing is relatively simple , That's it , If you think it's good , Please help me to praise , Thank you very much. !🥰🥰🥰
边栏推荐
- After Zotero beta 6.0 is installed, the problem that the built-in PDF reader cannot be used is solved
- Printk learning part 3: are you still using printk?
- JS obtient l'heure actuelle
- QQ微信实现连续发送消息【代码实现】
- vscode-markdown all in one-keyboard shortcut
- Task06: Autumn move script C
- QQ wechat enables continuous message sending [code implementation]
- Is it safe to open a stock account by mobile phone?
- The pipelineexecute pipeline execution process of VTK learning
- Build a bioinformatics r environment from 0 (step on the pit record)
猜你喜欢
随机推荐
New retail enterprises build smart marketing system
What is the name of one-dimensional array in C language
Data governance in industrial digital transformation
Pipeline pipeline for VTK learning
Ifstream seekg() read() text operation
基于深度学习的商品推荐系统(Web)
Want to be iron man? It is said that many big men use it to get started
What are the advantages of SaaS services
Build a bioinformatics r environment from 0 (step on the pit record)
Atom, the top stream editor, will leave the historical stage on December 15
Alignment HR_ MySQL logical architecture? That's it?
10 revelations from the success of digital transformation
从0开始刷力扣
在 Kubernetes 中基于 StatefulSet 部署 MySQL(上)
win11安装Pandoc
FormBuilder
Summary of MATLAB error reporting
案例分享 | 数智化升级:红蜻蜓的转型之路(下)
Wechat applet component observers [listener] uses this to report an error undefined
printk学习之(三):你还在用printk吗?










