三种情况,H和L都是偶数,或者都是奇数或者一个奇数一个偶数,对应三种情况。
class Solution:
def countOdds(self, low: int, high: int) -> int:
if low%2 and high%2:
return (high-1-low-1)//2 + 2
if not low%2 and not high%2:
return (high-low)//2
if (not low%2 and high%2) or (low%2 and not high%2):
return 1 + (high - 1 - low)//2