当前位置:网站首页>Li Kou daily question - day 29 -523 Count odd numbers in interval range
Li Kou daily question - day 29 -523 Count odd numbers in interval range
2022-06-28 03:37:00 【Chongyou research Sen】
2022.6.27 Did you brush the questions today
subject :
Here are two nonnegative integers low and high . Please return low and high Between ( Including both ) An odd number .
analysis :
Given an array , Find the number of odd numbers in this closed array , Note that you can take two numbers of array bounds .
Ideas : Let's go through this array , Returns the value when the element is odd ++, The final result .
analysis :
1. Violent solution
class Solution {
public:
int countOdds(int low, int high) {
int res = 0;
for (int i = low; i <= high; i++)
{
if (i % 2 != 0)
{
res++;
}
}
return res;
}
};2. The mathematical formula
because 【0-x】 Number of odd numbers in the interval prex(x)=((x+1)÷2). therefore 【x,y】 In the interval, we can use prex(y)-prex(x) Get
class Solution {
public:
int pre(int x)
{
return (x + 1) /2;
}
int countOdds(int low, int high) {
return pre(high) - pre(low-1);
}
};边栏推荐
- Floating point and complex type of go data type (4)
- Idea auto generate code
- 17 `bs object Node name h3 Parent ` parents get parent node ancestor node
- 数据库的迁移
- 项目实战!手把手教你 Jmeter 性能测试
- __ getitem__ And__ setitem__
- 第二轮红队免费公开课来袭~明晚八点!
- 2022电工(初级)复训题库及在线模拟考试
- 失联修复:让“躲猫猫”无处可藏
- Use js programming questions [split] in Niuke
猜你喜欢

数据库系列之MySQL中的分页查询优化

A16z: metauniverse unlocks new opportunities in game infrastructure

调试利器 go-spew

R1 Quick Open Pressure Vessel Operation Special Operation Certificate Examination Library and Answers in 2022

Websocket (simple experience version)

可扩展数据库(上)

GAMES104 作业2-ColorGrading

可扩展存储系统(上)

Relative path writing of files

根据Explain查看sql执行计划,对SQL进行优化
随机推荐
2022年R1快開門式壓力容器操作特種作業證考試題庫及答案
View the SQL execution plan according to explain and optimize the SQL
Go 數據類型篇(四)之浮點型與複數類型
劲爆!YOLOv6又快又准的目标检测框架开源啦(附源代码下载)
电子地图坐标系统研究整理
Excel知识技能汇总
数据库的迁移
Basic operation of stack (implemented in C language)
Idea auto generate code
Necessary software tools in embedded software development
为什么OpenCV计算的帧率是错误的?
Object类,以及__new__,__init__,__setattr__,__dict__
Change of monitoring documents and folders of "operation and maintenance department"
猴子都会用的圆形滑动自动吸附UI工具
nn.Parameter和torch.nn.init系列函数给模型参数初始化
INFO:&nbsp;HHH000397:&nbsp;Using…
R1 Quick Open Pressure Vessel Operation Special Operation Certificate Examination Library and Answers in 2022
国泰君安证券靠谱吗?开证券账户安全吗?
基于 WPF 的酷炫 GUI 窗口的简易实现
Ten reasons for system performance failure