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
相关推荐
小O的算法实验室18 小时前
2026年ASOC,基于深度强化学习的无人机三维复杂环境分层自适应导航规划方法,深度解析+性能实测
算法·无人机·论文复现·智能算法·智能算法改进
郭涤生20 小时前
STL vector 扩容机制与自定义内存分配器设计分析
c++·算法
༾冬瓜大侠༿20 小时前
vector
c语言·开发语言·数据结构·c++·算法
Ricky111zzz20 小时前
leetcode学python记录1
python·算法·leetcode·职场和发展
汀、人工智能20 小时前
[特殊字符] 第58课:两个正序数组的中位数
数据结构·算法·数据库架构··数据流·两个正序数组的中位数
liu****20 小时前
第16届省赛蓝桥杯大赛C/C++大学B组(京津冀)
开发语言·数据结构·c++·算法·蓝桥杯
汀、人工智能20 小时前
[特殊字符] 第79课:分割等和子集
数据结构·算法·数据库架构·位运算·哈希表·分割等和子集
汀、人工智能20 小时前
[特殊字符] 第74课:完全平方数
数据结构·算法·数据库架构·图论·bfs·完全平方数
CoderCodingNo20 小时前
【GESP】C++四、五级练习题 luogu-P1177 【模板】排序
数据结构·c++·算法
Proxy_ZZ020 小时前
从零实现LDPC比特翻转译码器:C语言实战与底层逻辑解析
c语言·算法