当前位置:网站首页>力扣每日一题-第29天-523.在区间范围统计奇数数目
力扣每日一题-第29天-523.在区间范围统计奇数数目
2022-06-28 02:43:00 【重邮研究森】
2022.6.27今天你刷题了吗
题目:
给你两个非负整数 low 和 high 。请你返回 low 和 high 之间(包括二者)奇数的数目。
分析:
给定一个数组,找到这个闭合数组里面奇数的个数,注意可以取数组边界的两个数。
思路:我们遍历这个数组,当元素为奇数时返回值++,最终得到结果。
解析:
1.暴力求解
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.数学公式
由于【0-x】区间内奇数个数prex(x)=((x+1)÷2).所以【x,y】区间内我们可以利用prex(y)-prex(x)求得
class Solution {
public:
int pre(int x)
{
return (x + 1) /2;
}
int countOdds(int low, int high) {
return pre(high) - pre(low-1);
}
};边栏推荐
- 数的每一位平方和
- Is Guotai Junan Securities reliable? Is it safe to open a securities account?
- Agileplm exception resolution session
- 调试利器 go-spew
- Win 10出现bitlocke恢复,蓝屏错误代码0x1600007e
- Go speed
- Use js programming questions [split] in Niuke
- Brief history and future trend of codeless software
- 剑指 Offer 53 - I. 在排序数组中查找数字 I(改进二分)
- WARN:&nbsp;SQL&nbsp;Error:&nbsp;…
猜你喜欢

剑指 Offer 49. 丑数(三指针法)
![[iptables & ICMP] description of ICMP Protocol in iptables default policy](/img/9d/85027ea0b0bc9c6494ba41daed9f38.png)
[iptables & ICMP] description of ICMP Protocol in iptables default policy

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

2022电工(初级)复训题库及在线模拟考试

matlab习题 —— 符号运算相关练习

云应用、服务的“5层”架构

mysql获取当前时间是一年的第多少天

17 `bs对象.节点名h3.parent` parents 获取父节点 祖先节点

Flow based depth generation model

数据库系列之InnoDB中在线DDL实现机制
随机推荐
TypeError:&nbsp;&#039;module&amp;#03…
自用工具 猴子都会用的unity视频播放器
劲爆!YOLOv6又快又准的目标检测框架开源啦(附源代码下载)
Introduction to kubernetes resource object and common commands
新手开哪家的证券账户是比较好?炒股开户安全吗
In the digital era, enterprises must do well in user information security
Apache——阿帕奇简介
collections. Use of defaultdict()
导入Excel文件,解决跳过空白单元格不读取,并且下标前移的问题,以及RETURN_BLANK_AS_NULL报红
电子地图坐标系统研究整理
2022 safety officer-c certificate examination question bank simulated examination platform operation
基于 LNMP 搭建个人网站的填坑之旅
云应用、服务的“5层”架构
Is Guotai Junan Securities reliable? Is it safe to open a securities account?
Brief history and future trend of codeless software
Question bank and answers of special operation certificate for R1 quick opening pressure vessel operation in 2022
【小程序】使用font-awesome字体图标的解决文案(图文)
MySQL 数据库的自动备份操作
Summary of the use of composition API in the project
Basic operation of stack (implemented in C language)