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
相关推荐
奶人五毛拉人一块4 小时前
动态规划--子数组类型
算法·动态规划·子数组问题
HugoStudio_SWAN5 小时前
洛谷 P1321 单词覆盖还原——从蜡板上的密信到数字水印
c++·学习·程序人生·算法
刘孬孬沉迷学习5 小时前
AI音频领域完整调研:任务、算法、大模型研究全梳理
人工智能·算法·音视频
退休倒计时5 小时前
【每日五题】leetcode TypeScript
算法·leetcode·职场和发展·typescript
机器学习之心5 小时前
基于BiGRU-Attention的轴承剩余寿命预测(MATLAB实现):从振动信号到RUL曲线的完整闭环
数据结构·算法·matlab·轴承剩余寿命预测·振动信号·bigru-attention
青梅橘子皮5 小时前
优选算法---专题2(滑动窗口)
数据结构·算法
程序猫.6 小时前
双指针问题
java·数据结构·算法
心抵鹊6 小时前
力扣每日一题:计算右侧小于当前元素的个数(hard)
算法·leetcode
鹿角片ljp6 小时前
LeetCode 142:环形链表 II |HashSet 保底解 + Floyd 快慢指针找环入口
算法·leetcode·链表
Fairy要carry6 小时前
初创实习-IMU模型
人工智能·算法·机器学习