当前位置:网站首页>[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
边栏推荐
- < script> detailed explanation of label content
- [acl2020] a novel method of component syntax tree serialization
- Mangodb simple to use
- [interprocess communication IPC] - semaphore learning
- 基于ArkUI eTS开发的坚果笑话(NutJoke)
- Can "Gulangyu yuancosmos" become an "upgraded sample" of China's cultural tourism industry
- As a VC, the auction house invested Web3 for the first time
- npm install报错 强制安装
- [daily algorithm 94] classic interview question: motion range of robot
- npm和yarn 更新依赖包
猜你喜欢

8 kinds of visual transformer finishing (Part 1)

Ctfshow ultimate assessment

对 int 变量赋值的操作是原子的吗?

【微服务~Sentinel】Sentinel之dashboard控制面板

ES6 new - array part

Music experience ceiling! Emotional design details of 14 Netease cloud music

苹果降价600元,对本就溃败的国产旗舰手机几乎是毁灭性打击

CUDA programming-05: flows and events

Data interaction based on restful pages

Antdesign a-modal自定义指令实现拖拽放大缩小
随机推荐
DNS域名空间
flex布局 (实战小米官网)
NPM and yarn update dependent packages
Built in method of tensorflow model training and evaluation
Antdesign a-modal自定义指令实现拖拽放大缩小
Programming style
How to optimize the deep learning model to improve the reasoning speed
【云原生之kubernetes实战】在kubernetes集群下部署Rainbond平台
如何注册码云账号
软件测试功能测试全套常见面试题【功能测试-零基础】必备4-1
博客怎么上传动态gif图
巴比特 | 元宇宙每日必读:广州南沙发布“元宇宙九条”措施,平台最高可获得2亿元资金支持...
坚果天气
基于ArkUI eTS开发的坚果笑话(NutJoke)
< script> detailed explanation of label content
Ztree custom title attribute
HUAWEI 机试题:字符串变换最小字符串 js
对 int 变量赋值的操作是原子的吗?
linux下安装oracle,本地PL/SQL连接Linux下的oracle导入表并新建用户和密码
苹果降价600元,对本就溃败的国产旗舰手机几乎是毁灭性打击