LeetCode 1523 Count Odd Numbers in an Interval Range (Python)

Posted by 小明MaxMing on February 13, 2023

题目

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

解题思路

(high-low)/2对于lowhigh四种不同的奇偶性,只有都是偶数的时候不需要+1

代码

class Solution:
    def countOdds(self, low: int, high: int) -> int:
        return (high - low) // 2 + (low % 2 == 1 or high % 2 == 1)

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅