[LeetCode]1523. Count Odd Numbers in an Interval Range (Easy) Python-2023.02.13
Posted on 2023-02-13 17:31 Uriel 閱讀(42) 評論(0) 編輯 收藏 引用 所屬分類: 數學 、閑來無事重切Leet Code問[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
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