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