当前位置:网站首页>[leetcode -- the first day of introduction to programming ability] basic data type [statistics of odd numbers within the range / average wage after removing the minimum wage and maximum wage)
[leetcode -- the first day of introduction to programming ability] basic data type [statistics of odd numbers within the range / average wage after removing the minimum wage and maximum wage)
2022-07-27 09:15:00 【Super Daxiong】
Catalog
subject : Count the odd number in the interval
Problem solving : Count the odd number in the interval
subject : The average wage after the minimum wage and the maximum wage are removed
subject : Count the odd number in the interval
Here are two nonnegative integers low and high . Please return low and high Between ( Including both ) An odd number .
Example 1:
Input :low = 3, high = 7
Output :3
explain :3 To 7 The odd number in between is [3,5,7] .
Example 2:Input :low = 8, high = 10
Output :1
explain :8 To 10 The odd number in between is [9] .Tips :
0 <= low <= high <= 10^9.
Error model :
class Solution {
public int countOdds(int low, int high) {
int sum=0;// Cumulative sum
// from low To hign Traverse in turn
for(int i=low;i<=high;i++){
// If it is judged to be odd, it will be added by itself
if(i%2!=0){
sum++;
}
}
return sum;
}
}The first thought of direct loop traversal is too lazy to use your brain , After clicking the test, I found Beyond the time limit . Remember, this should be an algorithm problem .
analysis :
I think so , Every number is an odd number 、 even numbers 、 Odd number 、 even numbers 、 This cycle . There are three possibilities low and height If both are odd numbers, use (high-low)/2+1, If both are even numbers (high-low)/2, If both are an odd number, an even number can be used (high-low+1)/2.
Both odd numbers :

Both are even numbers

Odd and even

Problem solving : Count the odd number in the interval
class Solution {
public int countOdds(int low, int high) {
if(low%2!=0&&high%2!=0){
return (high-low)/2+1;
}else if(low%2==0&&high%2==0){
return (high-low)/2;
}else if(low%2!=0&&high%2==0||low%2==0&&high%2!=0){
return (high-low+1)/2;
}
return 0;
}
}subject : The average wage after the minimum wage and the maximum wage are removed
Give you an array of integers salary , Every number in the array is only Of , among salary[i] It's No i The salary of an employee .
Please go back and remove the minimum wage and the maximum wage , The average wage of the remaining employees .
Example 1:
Input :salary = [4000,3000,1000,2000]
Output :2500.00000
explain : The minimum wage and the maximum wage are respectively 1000 and 4000 .
The average wage after removing the minimum wage and the maximum wage is (2000+3000)/2= 2500
Example 2:Input :salary = [1000,2000,3000]
Output :2000.00000
explain : The minimum wage and the maximum wage are respectively 1000 and 3000 .
The average wage after removing the minimum wage and the maximum wage is (2000)/1= 2000
Example 3:Input :salary = [6000,5000,4000,3000,2000,1000]
Output :3500.00000
Example 4:Input :salary = [8000,9000,2000,3000,6000,1000]
Output :4750.00000
Tips :
3 <= salary.length <= 100
10^3 <= salary[i] <= 10^6
salary[i] Is the only one. .
The error with the true value is within 10^-5 All the results within will be regarded as the correct answer .
Error model :
No,
analysis : We use Arrays class Of sort The sorting method is OK for Loop traversal can also be done without problems
For Traverse
class Solution {
public double average(int[] salary) {
double min=salary[0];// Storage minimum
double max=salary[0];// Storage maximum
double sum=0;// Store accumulation and
for(int i=0;i<salary.length;i++){
if(salary[i]>max){
max=salary[i];
}
if(salary[i]<min){
min=salary[i];
}
// Accumulate and store
sum+=salary[i];
}
// Subtract the maximum and minimum values
sum=sum-max-min;
return sum/(salary.length-2);
}
}Arrays class
class Solution {
public double average(int[] salary) {
// Sort automatically
Arrays.sort(salary);
// Store the accumulated value
double a=0;
// The maximum and minimum values are set to 0
salary[0]=0;
salary[salary.length-1]=0;
for(int i=0;i<salary.length;i++){
a+=salary[i];
}
return a/(salary.length-2);
}
}CSDN Community 《 Creative talent 》 Activities , As long as you participate in it and write articles, you will have the opportunity to win official prizes : Boutique calendar 、 New programmer magazine , Let's get involved ! Link directly to https://bbs.csdn.net/topics/605272551
边栏推荐
- Specific methods and steps for Rockwell AB PLC to establish communication with PLC through rslinx classic
- BEVFormer: Learning Bird’s-Eye-View Representation from Multi-Camera Images via Spatiotemporal Trans
- openharmony萌新贡献指南
- Pymongo fuzzy query
- 8 kinds of visual transformer finishing (Part 2)
- ctfshow 终极考核
- Pyqt5 rapid development and practice 4.1 qmainwindow
- 【微服务~Sentinel】Sentinel之dashboard控制面板
- The lifecycle of arkui development framework components
- Qdoublevalidator does not take effect solution
猜你喜欢

Flex layout (actual Xiaomi official website)

ES6 new - array part

08_ Service fusing hystrix

PVT的spatial reduction attention(SRA)

How to deploy yolov6 with tensorrt

Mangodb simple to use

Mangodb简单使用

Pytorch custom CUDA operator tutorial and runtime analysis

CUDA programming-05: flows and events

【进程间通信IPC】- 信号量的学习
随机推荐
易语言编程: 让读屏软件可获取标签控件的文本
音乐体验天花板!14个网易云音乐的情感化设计细节
Explanation of binary tree
07_ Service registration and discovery summary
[cloud co creation] Huawei cloud: full stack technology innovation, deep digitalization, leading cloud native
New year's goals! The code is more standardized!
Specific methods and steps for Rockwell AB PLC to establish communication with PLC through rslinx classic
Activation functions commonly used in deep learning
基于ArkUI eTS开发的坚果笑话(NutJoke)
flex布局 (实战小米官网)
500 error reporting
苹果降价600元,对本就溃败的国产旗舰手机几乎是毁灭性打击
Five kinds of 3D attention/transformer finishing (a-scn, point attention, CAA, offset attention, point transformer)
SQL exercise set
Tensorflow loss function
Restful
HUAWEI 机试题:火星文计算 js
DNS域名空间
CUDA programming-02: first knowledge of CUDA Programming
Rewrite the tensorrt version deployment code of yolox