File tree Expand file tree Collapse file tree 7 files changed +78
-9
lines changed
solution/1500-1599/1523.Count Odd Numbers in an Interval Range Expand file tree Collapse file tree 7 files changed +78
-9
lines changed Original file line number Diff line number Diff line change 34
34
35
35
<!-- 这里可写通用的实现逻辑 -->
36
36
37
- 计算 ` low ` 与 ` high ` 之间的差值,折半得解。由于包括边界在内,两个边界数有一个为奇数,那么需要对折半结果 + 1。
37
+ ** 方法一:前缀和思想**
38
+
39
+ ` [0, x] ` 之间的奇数个数为 ` (x + 1) >> 1 ` ,那么 ` [low, high] ` 之间的奇数个数为 ` ((high + 1) >> 1) - (low >> 1) ` 。
38
40
39
41
<!-- tabs:start -->
40
42
43
45
<!-- 这里可写当前语言的特殊实现逻辑 -->
44
46
45
47
``` python
46
-
48
+ class Solution :
49
+ def countOdds (self , low : int , high : int ) -> int :
50
+ return ((high + 1 ) >> 1 ) - (low >> 1 )
47
51
```
48
52
49
53
### ** Java**
50
54
51
55
<!-- 这里可写当前语言的特殊实现逻辑 -->
52
56
53
57
``` java
54
-
58
+ class Solution {
59
+ public int countOdds (int low , int high ) {
60
+ return ((high + 1 ) >> 1 ) - (low >> 1 );
61
+ }
62
+ }
55
63
```
56
64
57
65
### ** Rust**
58
66
59
67
``` rust
60
68
impl Solution {
61
69
pub fn count_odds (low : i32 , high : i32 ) -> i32 {
62
- (high - low ) / 2 + if low & 1 == 1 || high & 1 == 1 { 1 } else { 0 }
70
+ (( high + 1 ) >> 1 ) - ( low >> 1 )
63
71
}
64
72
}
65
73
```
66
74
75
+ ### ** C++**
76
+
77
+ ``` cpp
78
+ class Solution {
79
+ public:
80
+ int countOdds(int low, int high) {
81
+ return (high + 1 >> 1) - (low >> 1);
82
+ }
83
+ };
84
+ ```
85
+
86
+ ### **Go**
87
+
88
+ ```go
89
+ func countOdds(low int, high int) int {
90
+ return ((high + 1) >> 1) - (low >> 1)
91
+ }
92
+ ```
93
+
67
94
### ** ...**
68
95
69
96
```
Original file line number Diff line number Diff line change 44
44
### ** Python3**
45
45
46
46
``` python
47
-
47
+ class Solution :
48
+ def countOdds (self , low : int , high : int ) -> int :
49
+ return ((high + 1 ) >> 1 ) - (low >> 1 )
48
50
```
49
51
50
52
### ** Java**
51
53
52
54
``` java
53
-
55
+ class Solution {
56
+ public int countOdds (int low , int high ) {
57
+ return ((high + 1 ) >> 1 ) - (low >> 1 );
58
+ }
59
+ }
54
60
```
55
61
56
62
### ** Rust**
57
63
58
64
``` rust
59
65
impl Solution {
60
66
pub fn count_odds (low : i32 , high : i32 ) -> i32 {
61
- (high - low ) / 2 + if low & 1 == 1 || high & 1 == 1 { 1 } else { 0 }
67
+ ((high + 1 ) >> 1 ) - (low >> 1 )
68
+ }
69
+ }
70
+ ```
71
+
72
+ ### ** C++**
73
+
74
+ ``` cpp
75
+ class Solution {
76
+ public:
77
+ int countOdds(int low, int high) {
78
+ return (high + 1 >> 1) - (low >> 1);
62
79
}
80
+ };
81
+ ```
82
+
83
+ ### **Go**
84
+
85
+ ```go
86
+ func countOdds(low int, high int) int {
87
+ return ((high + 1) >> 1) - (low >> 1)
63
88
}
64
89
```
65
90
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int countOdds (int low, int high) {
4
+ return (high + 1 >> 1 ) - (low >> 1 );
5
+ }
6
+ };
Original file line number Diff line number Diff line change
1
+ func countOdds (low int , high int ) int {
2
+ return ((high + 1 ) >> 1 ) - (low >> 1 )
3
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int countOdds (int low , int high ) {
3
+ return ((high + 1 ) >> 1 ) - (low >> 1 );
4
+ }
5
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def countOdds (self , low : int , high : int ) -> int :
3
+ return ((high + 1 ) >> 1 ) - (low >> 1 )
Original file line number Diff line number Diff line change 1
1
impl Solution {
2
2
pub fn count_odds ( low : i32 , high : i32 ) -> i32 {
3
- ( high - low ) / 2 + if low & 1 == 1 || high & 1 == 1 { 1 } else { 0 }
3
+ ( ( high + 1 ) >> 1 ) - ( low >> 1 )
4
4
}
5
- }
5
+ }
You can’t perform that action at this time.
0 commit comments