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
相关推荐
IT猿手5 分钟前
SCI一区:章鱼优化算法(Octopus Optimization Algorithm, OOA)求解23个测试函数,出图丰富,提供完整MATLAB代码
开发语言·算法·matlab
superior tigre6 分钟前
739 每日温度
算法·leetcode·职场和发展
忡黑梨10 分钟前
eNSP_从直连到BGP全网互通
c语言·网络·数据结构·python·算法·网络安全
Run_Teenage25 分钟前
算法:离散化模板
算法
乐迪信息25 分钟前
乐迪信息:实时预警,秒级响应:船舶AI异常行为检测算法
大数据·人工智能·算法·安全·目标跟踪
6Hzlia27 分钟前
【Hot 100 刷题计划】 LeetCode 15. 三数之和 | C++ 排序+双指针
c++·算法·leetcode
fox_lht30 分钟前
第十章 通用集合
开发语言·后端·算法·rust
新新学长搞科研38 分钟前
【高质量能源会议推荐】第十一届能源与环境研究进展国际学术会议(ICAEER 2026)
人工智能·物联网·算法·机器学习·能源·环境·新能源
CN-Dust1 小时前
【C++】for循环例题专题
java·c++·算法
楼兰公子1 小时前
读取rpi摄像头
linux·服务器·算法