You are given two non-negative integers low and high. Return the count of odd numbers between low and high (inclusive).
Example 1:
Input: low = 2, high = 9
Output: 4Explanation: The odd numbers between 2 and 9 are [3,5,7,9].
Example 2:
Input: low = 9, high = 11
Output: 2Explanation: The odd numbers between 9 and 11 are [9,11].
Example 2:
Input: low = 1, high = 1
Output: 1Constraints:
0 <= low <= high <= 1,000,000,000class Solution:
def countOdds(self, low: int, high: int) -> int:
odd = 0
for num in range(low, high + 1):
if num & 1:
odd += 1
return oddWhere is the number of integers in the given range.
class Solution:
def countOdds(self, low: int, high: int) -> int:
length = high - low + 1
count = length // 2
if length % 2 and low % 2:
count += 1
return countclass Solution:
def countOdds(self, low: int, high: int) -> int:
return ((high + 1) >> 1) - (low >> 1)