問題
原文
Given two non-negative integers
low
andhigh
. Return the count of odd numbers betweenlow
andhigh
(inclusive).
Example 1:
123 Input: low = 3, high = 7Output: 3Explanation: The odd numbers between 3 and 7 are [3,5,7].Example 2:
123 Input: low = 8, high = 10Output: 1Explanation: The odd numbers between 8 and 10 are [9].
Constraints:
0 <= low <= high <= 10^9
内容
2つの非負整数low,highが与えられるので、lowとhighの間にある奇数の数を返してください。
※正しくない可能性があります。
解答
解答1:Python
1 2 3 4 5 6 7 8 9 10 |
class Solution: def countOdds(self, low: int, high: int) -> int: #lowかhighのどちらかが奇数の場合 if low%2!=0 or high%2!=0: #highからlowを引いた数を2で割り、さらに1を加えた数を返す return (high-low)//2 + 1 #high,lowのどちらも偶数の場合 else: #highからlowを引いた数を2で割った数字を返す return (high-low)//2 |
解答2:
メモ・参考・感想
■メモ
odd number:奇数
次:1491. Average Salary Excluding the Minimum and Maximum Salary
コメント