当前位置:网站首页>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);
}
};边栏推荐
- 猴子都会用的圆形滑动自动吸附UI工具
- Redis搭建集群【简单】
- Ten years' experience of Software Engineer
- What is the difference between slice and array in go question bank 12?
- matlab习题 —— 数据的基本处理
- matlab习题 —— 矩阵的常规运算
- matlab习题 —— 符号运算相关练习
- Dataloader parameter collate_ Use of FN
- Tardigrade:Trino 解决 ETL 场景的方案
- ETCD数据库源码分析——集群间网络层服务端RaftHandler
猜你喜欢
随机推荐
A16z: metauniverse unlocks new opportunities in game infrastructure
17 `bs对象.节点名h3.parent` parents 获取父节点 祖先节点
华为设备WLAN基本业务配置命令
剑指 Offer 47. 礼物的最大价值(DP)
Is Guotai Junan Securities reliable? Is it safe to open a securities account?
ARM Development Studio build编译报错
Ten years' experience of Software Engineer
开口式霍尔电流传感器如何助力直流配电改造?
【PaddleDetection】ModuleNotFoundError: No module named ‘paddle‘
__getitem__和__setitem__
Severe Tire Damage:世界上第一个在互联网上直播的摇滚乐队
What are the good practices of cloud cost optimization?
猴子都会用的圆形滑动自动吸附UI工具
文件的相对路径写法
"Five layer" architecture of cloud applications and services
s32ds跳转到DefaultISR
matlab习题 —— 数据的基本处理
音视频技术开发周刊 | 251
可扩展数据库(下)
導入Excel文件,解决跳過空白單元格不讀取,並且下標前移的問題,以及RETURN_BLANK_AS_NULL報紅








