Skip to content

Commit 9e17960

Browse files
committed
feat: add solutions to lc problem: No.1523
No.1523.Count Odd Numbers in an Interval Range
1 parent f690dad commit 9e17960

File tree

7 files changed

+78
-9
lines changed

7 files changed

+78
-9
lines changed

solution/1500-1599/1523.Count Odd Numbers in an Interval Range/README.md

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434

3535
<!-- 这里可写通用的实现逻辑 -->
3636

37-
计算 `low``high` 之间的差值,折半得解。由于包括边界在内,两个边界数有一个为奇数,那么需要对折半结果 + 1。
37+
**方法一:前缀和思想**
38+
39+
`[0, x]` 之间的奇数个数为 `(x + 1) >> 1`,那么 `[low, high]` 之间的奇数个数为 `((high + 1) >> 1) - (low >> 1)`
3840

3941
<!-- tabs:start -->
4042

@@ -43,27 +45,52 @@
4345
<!-- 这里可写当前语言的特殊实现逻辑 -->
4446

4547
```python
46-
48+
class Solution:
49+
def countOdds(self, low: int, high: int) -> int:
50+
return ((high + 1) >> 1) - (low >> 1)
4751
```
4852

4953
### **Java**
5054

5155
<!-- 这里可写当前语言的特殊实现逻辑 -->
5256

5357
```java
54-
58+
class Solution {
59+
public int countOdds(int low, int high) {
60+
return ((high + 1) >> 1) - (low >> 1);
61+
}
62+
}
5563
```
5664

5765
### **Rust**
5866

5967
```rust
6068
impl Solution {
6169
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)
6371
}
6472
}
6573
```
6674

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+
6794
### **...**
6895

6996
```

solution/1500-1599/1523.Count Odd Numbers in an Interval Range/README_EN.md

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,47 @@
4444
### **Python3**
4545

4646
```python
47-
47+
class Solution:
48+
def countOdds(self, low: int, high: int) -> int:
49+
return ((high + 1) >> 1) - (low >> 1)
4850
```
4951

5052
### **Java**
5153

5254
```java
53-
55+
class Solution {
56+
public int countOdds(int low, int high) {
57+
return ((high + 1) >> 1) - (low >> 1);
58+
}
59+
}
5460
```
5561

5662
### **Rust**
5763

5864
```rust
5965
impl Solution {
6066
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);
6279
}
80+
};
81+
```
82+
83+
### **Go**
84+
85+
```go
86+
func countOdds(low int, high int) int {
87+
return ((high + 1) >> 1) - (low >> 1)
6388
}
6489
```
6590

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution {
2+
public:
3+
int countOdds(int low, int high) {
4+
return (high + 1 >> 1) - (low >> 1);
5+
}
6+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
func countOdds(low int, high int) int {
2+
return ((high + 1) >> 1) - (low >> 1)
3+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution {
2+
public int countOdds(int low, int high) {
3+
return ((high + 1) >> 1) - (low >> 1);
4+
}
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def countOdds(self, low: int, high: int) -> int:
3+
return ((high + 1) >> 1) - (low >> 1)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
impl Solution {
22
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)
44
}
5-
}
5+
}

0 commit comments

Comments
 (0)