Skip to content

Commit 50aa399

Browse files
committed
update: 添加问题“1523.在区间范围内统计奇数数目”的代码和题解 + test newSolutions.py(remove header) + test Tisfy on WindowsG3 (#1399)
1523: AC.cpp (#1261)(#1399) cpp - AC,100.00%,62.72% py - AC,100.00%,7.10% go - AC,100.00%,65.85% java - AC,100.00%,68.02% rust - AC,100.00%,13.30% Signed-off-by: Tisfy <Tisfy@foxmail.com>
1 parent 9059bb0 commit 50aa399

13 files changed

Lines changed: 479 additions & 5 deletions

.commitTitleExtra

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
+ test newSolutions.py(remove header) + test Tisfy on WindowsG3

.commitmsg

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
1523: AC.cpp (#1261)(#1399)
2+
3+
cpp - AC,100.00%,62.72%
4+
py - AC,100.00%,7.10%
5+
go - AC,100.00%,65.85%
6+
java - AC,100.00%,68.02%
7+
rust - AC,100.00%,13.30%
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2026-02-19 17:38:02
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2026-02-19 17:39:31
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
int countOdds(int low, int high) {
14+
if (low % 2) {
15+
low--;
16+
}
17+
if (high % 2) {
18+
high++;
19+
}
20+
return (high - low) >> 1;
21+
}
22+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2026-02-19 17:59:08
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2026-02-19 18:02:40
6+
*/
7+
package main
8+
9+
func countOdds(low int, high int) int {
10+
if low % 2 == 1 {
11+
low--
12+
}
13+
if high % 2 == 1 {
14+
high++
15+
}
16+
return (high - low) / 2
17+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2026-02-19 17:59:08
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2026-02-19 18:04:04
6+
*/
7+
class Solution {
8+
public int countOdds(int low, int high) {
9+
return (high + 1) / 2 - low / 2;
10+
}
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2026-02-19 17:59:08
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2026-02-19 18:00:11
6+
'''
7+
class Solution:
8+
def countOdds(self, low: int, high: int) -> int:
9+
if low % 2:
10+
low -= 1
11+
if high % 2:
12+
high += 1
13+
return (high - low) // 2
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2026-02-19 17:59:08
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2026-02-19 18:06:07
6+
*/
7+
impl Solution {
8+
pub fn count_odds(low: i32, high: i32) -> i32 {
9+
(high + 1) / 2 - low / 2
10+
}
11+
}

Codes/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77
pub struct Solution;
88

9-
include!("0696-count-binary-substrings.rs"); // 这个fileName是会被脚本替换掉的
9+
include!("1523-count-odd-numbers-in-an-interval-range_20260219.rs"); // 这个fileName是会被脚本替换掉的
1010

1111
#[derive(Debug, PartialEq, Eq)]
1212
pub struct TreeNode {

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,7 @@
598598
|1488.避免洪水泛滥|中等|<a href="https://leetcode.cn/problems/avoid-flood-in-the-city/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/10/13/LeetCode%201488.%E9%81%BF%E5%85%8D%E6%B4%AA%E6%B0%B4%E6%B3%9B%E6%BB%A5/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/133809999" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/avoid-flood-in-the-city/solutions/2480771/letmefly-1488bi-mian-hong-shui-fan-lan-h-yxx5/" target="_blank">LeetCode题解</a>|
599599
|1491.去掉最低工资和最高工资后的工资平均值|简单|<a href="https://leetcode.cn/problems/average-salary-excluding-the-minimum-and-maximum-salary/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/05/03/LeetCode%201491.%E5%8E%BB%E6%8E%89%E6%9C%80%E4%BD%8E%E5%B7%A5%E8%B5%84%E5%92%8C%E6%9C%80%E9%AB%98%E5%B7%A5%E8%B5%84%E5%90%8E%E7%9A%84%E5%B7%A5%E8%B5%84%E5%B9%B3%E5%9D%87%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/138416153" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/average-salary-excluding-the-minimum-and-maximum-salary/solutions/2764238/letmefly-1491qu-diao-zui-di-gong-zi-he-z-8h25/" target="_blank">LeetCode题解</a>|
600600
|1499.满足不等式的最大值|困难|<a href="https://leetcode.cn/problems/max-value-of-equation/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/07/21/LeetCode%201499.%E6%BB%A1%E8%B6%B3%E4%B8%8D%E7%AD%89%E5%BC%8F%E7%9A%84%E6%9C%80%E5%A4%A7%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/131844105" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/max-value-of-equation/solutions/2352476/letmefly-1499man-zu-bu-deng-shi-de-zui-d-wlzu/" target="_blank">LeetCode题解</a>|
601+
|1523.在区间范围内统计奇数数目|简单|<a href="https://leetcode.cn/problems/count-odd-numbers-in-an-interval-range/" target="_blank">题目地址</a>|<a href="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/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/158209747" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-odd-numbers-in-an-interval-range/solutions/3904901/letmefly-1523zai-qu-jian-fan-wei-nei-ton-jquc/" target="_blank">LeetCode题解</a>|
601602
|1534.统计好三元组|简单|<a href="https://leetcode.cn/problems/count-good-triplets/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/04/15/LeetCode%201534.%E7%BB%9F%E8%AE%A1%E5%A5%BD%E4%B8%89%E5%85%83%E7%BB%84/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147249275" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-good-triplets/solutions/3651795/letmefly-1534tong-ji-hao-san-yuan-zu-xi-dv8dw/" target="_blank">LeetCode题解</a>|
602603
|1535.找出数组游戏的赢家|中等|<a href="https://leetcode.cn/problems/find-the-winner-of-an-array-game/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/05/19/LeetCode%201535.%E6%89%BE%E5%87%BA%E6%95%B0%E7%BB%84%E6%B8%B8%E6%88%8F%E7%9A%84%E8%B5%A2%E5%AE%B6/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/139040126" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-the-winner-of-an-array-game/solutions/2782688/letmefly-1535zhao-chu-shu-zu-you-xi-de-y-ff3w/" target="_blank">LeetCode题解</a>|
603604
|1542.找出最长的超赞子字符串|困难|<a href="https://leetcode.cn/problems/find-longest-awesome-substring/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/05/20/LeetCode%201542.%E6%89%BE%E5%87%BA%E6%9C%80%E9%95%BF%E7%9A%84%E8%B6%85%E8%B5%9E%E5%AD%90%E5%AD%97%E7%AC%A6%E4%B8%B2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/139077950" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-longest-awesome-substring/solutions/2784697/letmefly-1542zhao-chu-zui-chang-de-chao-16nco/" target="_blank">LeetCode题解</a>|
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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>给你两个非负整数&nbsp;<code>low</code> 和&nbsp;<code>high</code>&nbsp;。请你返回<em>&nbsp;</em><code>low</code><em> </em>和<em>&nbsp;</em><code>high</code><em>&nbsp;</em>之间(包括二者)奇数的数目。</p>
13+
14+
<p>&nbsp;</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>&nbsp;</p>
29+
30+
<p><strong>提示:</strong></p>
31+
32+
<ul>
33+
<li><code>0 &lt;= low &lt;= high&nbsp;&lt;= 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

Comments
 (0)