又是一道莫名其妙做出的题
1.思路
正常人想法,循环遍历,统计奇数个数,提交发现超时。看提示后惊觉可以总结规律,分为两种情况讨论。
2.解题方法
见代码。
3.Code
class Solution:
def countOdds(self, low: int, high: int) -> int:
if (high-low+1)%2==0:
return (high-low+1)//2
else:
if low%2==0 and high%2==0:
return (high-low)//2
else:
return (high-low-1)//2+2