1523.Count Odd Numbers in an Interval Range
===
###### tags: `Easy`,`Math`
[1523. Count Odd Numbers in an Interval Range](https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/)
### 題目描述
Given two non-negative integers `low` and `high`. Return *the count of odd numbers between* `low` *and* `high` *(inclusive).*
### 範例
**Example 1:**
```
Input: low = 3, high = 7
Output: 3
Explanation: The odd numbers between 3 and 7 are [3,5,7].
```
**Example 2:**
```
Input: low = 8, high = 10
Output: 1
Explanation: The odd numbers between 8 and 10 are [9].
```
**Constraints**:
* 0 <= `low` <= `high` <= 10^9^
### 解答
#### Javascript
```javascript=
function countOdds(low, high) {
if (low % 2 === 0 && high % 2 === 0) return (high - low) / 2;
return ~~((high - low) / 2) + 1;
}
```
> 沒記錯好像是programiming skills 1第一天的題目
> 跟吉神學習惹 (看下面那個)
> [name=Marsgoat] [time= Feb 13, 2023]
```javascript=
var countOdds = function(low, high) {
return Math.floor((high + 1) / 2) - Math.floor(low / 2);
};
```
> [name=Yen-Chi Chen][time=Mon, Feb 13, 2023]
#### C++
##### 思路
[1, high]之間的奇數數量 - [1, low-1]之間的奇數數量
```cpp=
class Solution {
public:
int countOdds(int low, int high) {
return (high + 1) / 2 - low / 2;
}
};
```
> [name=Yen-Chi Chen][time=Mon, Feb 13, 2023]
#### Python
```python=
class Solution:
def countOdds(self, low: int, high: int) -> int:
return (high - low) // 2 + (low % 2 == 1 or high % 2 == 1)
```
> [name=Ron Chen][time= Feb 13, 2023]
```python=
class Solution:
def countOdds(self, low: int, high: int) -> int:
return (high + 1) // 2 - low // 2
```
> [name=Yen-Chi Chen][time=Mon, Feb 13, 2023]
#### C#
```csharp=
public class Solution {
public int CountOdds(int low, int high) {
return (high - low) / 2 + ((low | high) & 1);
}
}
```
> [name=Jim][time=Feb 13, 2023]
### Reference
[回到題目列表](https://hackmd.io/@Marsgoat/leetcode_every_day)