|
| 1 | +--- |
| 2 | +title: 1523.在区间范围内统计奇数数目:两种方法O(1)算 |
| 3 | +date: 2026-02-19 18:19:46 |
| 4 | +tags: [题解, LeetCode, 简单, 数学] |
| 5 | +categories: [题解, LeetCode] |
| 6 | +--- |
| 7 | + |
| 8 | +# 【LetMeFly】1523.在区间范围内统计奇数数目:两种方法O(1)算 |
| 9 | + |
| 10 | +力扣题目链接:[https://leetcode.cn/problems/count-odd-numbers-in-an-interval-range/](https://leetcode.cn/problems/count-odd-numbers-in-an-interval-range/) |
| 11 | + |
| 12 | +<p>给你两个非负整数 <code>low</code> 和 <code>high</code> 。请你返回<em> </em><code>low</code><em> </em>和<em> </em><code>high</code><em> </em>之间(包括二者)奇数的数目。</p> |
| 13 | + |
| 14 | +<p> </p> |
| 15 | + |
| 16 | +<p><strong>示例 1:</strong></p> |
| 17 | + |
| 18 | +<pre><strong>输入:</strong>low = 3, high = 7 |
| 19 | +<strong>输出:</strong>3 |
| 20 | +<strong>解释:</strong>3 到 7 之间奇数数字为 [3,5,7] 。</pre> |
| 21 | + |
| 22 | +<p><strong>示例 2:</strong></p> |
| 23 | + |
| 24 | +<pre><strong>输入:</strong>low = 8, high = 10 |
| 25 | +<strong>输出:</strong>1 |
| 26 | +<strong>解释:</strong>8 到 10 之间奇数数字为 [9] 。</pre> |
| 27 | + |
| 28 | +<p> </p> |
| 29 | + |
| 30 | +<p><strong>提示:</strong></p> |
| 31 | + |
| 32 | +<ul> |
| 33 | + <li><code>0 <= low <= high <= 10^9</code></li> |
| 34 | +</ul> |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | +## 解题方法一:思考容易写着麻烦点 |
| 39 | + |
| 40 | +low和high都是偶数的示例 2:low = 8, high = 10,中间共有几个奇数? |
| 41 | + |
| 42 | +答案是$\frac{10-8}2$个。 |
| 43 | + |
| 44 | +如果low是奇数,那么我们令low-1;如果high是奇数,那么我们就令high+1就好了。 |
| 45 | + |
| 46 | ++ 时间复杂度$O(1)$ |
| 47 | ++ 空间复杂度$O(1)$ |
| 48 | + |
| 49 | +### AC代码 |
| 50 | + |
| 51 | +#### C++ |
| 52 | + |
| 53 | +```cpp |
| 54 | +/* |
| 55 | + * @LastEditTime: 2026-02-19 17:39:31 |
| 56 | + */ |
| 57 | +class Solution { |
| 58 | +public: |
| 59 | + int countOdds(int low, int high) { |
| 60 | + if (low % 2) { |
| 61 | + low--; |
| 62 | + } |
| 63 | + if (high % 2) { |
| 64 | + high++; |
| 65 | + } |
| 66 | + return (high - low) >> 1; |
| 67 | + } |
| 68 | +}; |
| 69 | +``` |
| 70 | +
|
| 71 | +#### Python |
| 72 | +
|
| 73 | +```python |
| 74 | +''' |
| 75 | +LastEditTime: 2026-02-19 18:00:11 |
| 76 | +''' |
| 77 | +class Solution: |
| 78 | + def countOdds(self, low: int, high: int) -> int: |
| 79 | + if low % 2: |
| 80 | + low -= 1 |
| 81 | + if high % 2: |
| 82 | + high += 1 |
| 83 | + return (high - low) // 2 |
| 84 | +``` |
| 85 | + |
| 86 | +#### Go |
| 87 | + |
| 88 | +```go |
| 89 | +/* |
| 90 | + * @LastEditTime: 2026-02-19 18:02:40 |
| 91 | + */ |
| 92 | +package main |
| 93 | + |
| 94 | +func countOdds(low int, high int) int { |
| 95 | + if low % 2 == 1 { |
| 96 | + low-- |
| 97 | + } |
| 98 | + if high % 2 == 1 { |
| 99 | + high++ |
| 100 | + } |
| 101 | + return (high - low) / 2 |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +## 解题方法二:思考麻烦点写着容易 |
| 106 | + |
| 107 | +不论一个数的奇偶性: |
| 108 | + |
| 109 | +* $[1, low)$ 共有几个奇数?答案是$\lfloor\frac{low}2\rfloor$个; |
| 110 | +* $[1, high]$ 共有几个奇数?答案是$\lfloor\frac{high+1}2\rfloor$个。 |
| 111 | + |
| 112 | +那么 $[low, high]$ 共有几个奇数? $[1, high] - [1, low)$ 即为所得。 |
| 113 | + |
| 114 | ++ 时间复杂度$O(1)$ |
| 115 | ++ 空间复杂度$O(1)$ |
| 116 | + |
| 117 | +### AC代码 |
| 118 | + |
| 119 | +#### Java |
| 120 | + |
| 121 | +```java |
| 122 | +/* |
| 123 | + * @LastEditTime: 2026-02-19 18:04:04 |
| 124 | + */ |
| 125 | +class Solution { |
| 126 | + public int countOdds(int low, int high) { |
| 127 | + return (high + 1) / 2 - low / 2; |
| 128 | + } |
| 129 | +} |
| 130 | +``` |
| 131 | + |
| 132 | +#### Rust |
| 133 | + |
| 134 | +```rust |
| 135 | +/* |
| 136 | + * @LastEditTime: 2026-02-19 18:06:07 |
| 137 | + */ |
| 138 | +impl Solution { |
| 139 | + pub fn count_odds(low: i32, high: i32) -> i32 { |
| 140 | + (high + 1) / 2 - low / 2 |
| 141 | + } |
| 142 | +} |
| 143 | +``` |
| 144 | + |
| 145 | +> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/158209747)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2026/02/19/LeetCode%201523.%E5%9C%A8%E5%8C%BA%E9%97%B4%E8%8C%83%E5%9B%B4%E5%86%85%E7%BB%9F%E8%AE%A1%E5%A5%87%E6%95%B0%E6%95%B0%E7%9B%AE/)哦~ |
| 146 | +> |
| 147 | +> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode) |
0 commit comments