问[low high]区间内有多少基数,简单数学题,O(1)
1 #1523
2 #Runtime: 22 ms (Beats 34.12%)
3 #Memory: 13.2 MB (Beats 98.78%)
4
5 class Solution(object):
6 def countOdds(self, low, high):
7 """
8 :type low: int
9 :type high: int
10 :rtype: int
11 """
12 ans = 0
13 if low < high:
14 ans = (high - low + 1) // 2
15 if low % 2 and high % 2:
16 ans += 1
17 return ans