Leetcode 1523. Count Odd Numbers in an Interval Range

Problem

Given two non-negative integers low and high. Return the count of odd numbers between low and high (inclusive).

Algorithm

Count the number of odd integers between low and high. Find the nearest low_odd and high_odd, then calculate (high_odd - low_odd) // 2 + 1.

Code

python3 复制代码
class Solution:
    def countOdds(self, low: int, high: int) -> int:
        if low % 2 == 0:
            low += 1
        if high % 2 == 0:
            high -= 1
        
        return (high - low) // 2 + 1
相关推荐
爱吃rabbit的mq13 小时前
第09章:随机森林:集成学习的威力
算法·随机森林·集成学习
(❁´◡`❁)Jimmy(❁´◡`❁)14 小时前
Exgcd 学习笔记
笔记·学习·算法
YYuCChi14 小时前
代码随想录算法训练营第三十七天 | 52.携带研究材料(卡码网)、518.零钱兑换||、377.组合总和IV、57.爬楼梯(卡码网)
算法·动态规划
不能隔夜的咖喱14 小时前
牛客网刷题(2)
java·开发语言·算法
VT.馒头14 小时前
【力扣】2721. 并行执行异步函数
前端·javascript·算法·leetcode·typescript
进击的小头15 小时前
实战案例:51单片机低功耗场景下的简易滤波实现
c语言·单片机·算法·51单片机
咖丨喱16 小时前
IP校验和算法解析与实现
网络·tcp/ip·算法
罗湖老棍子16 小时前
括号配对(信息学奥赛一本通- P1572)
算法·动态规划·区间dp·字符串匹配·区间动态规划
fengfuyao98517 小时前
基于MATLAB的表面织构油润滑轴承故障频率提取(改进VMD算法)
人工智能·算法·matlab
机器学习之心17 小时前
基于随机森林模型的轴承剩余寿命预测MATLAB实现!
算法·随机森林·matlab