Skip to content

Commit 9e449f7

Browse files
solves #2595: Number of Even and Odd Bits in java
1 parent 1ac1592 commit 9e449f7

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@
808808
| 2582 | [Pass the Pillow](https://leetcode.com/problems/pass-the-pillow) | [![Java](assets/java.png)](src/PassThePillow.java) | |
809809
| 2586 | [Count the Number of Vowel Strings in Range](https://leetcode.com/problems/count-the-number-of-vowel-strings-in-range) | [![Java](assets/java.png)](src/CountTheNumberOfVowelStringsInRange.java) | |
810810
| 2591 | [Distribute Money to Maximum Children](https://leetcode.com/problems/distribute-money-to-maximum-children) | [![Java](assets/java.png)](src/DistributeMoneyToMaximumChildren.java) | |
811-
| 2595 | [Number of Even and Odd Bits](https://leetcode.com/problems/number-of-even-and-odd-bits) | | |
811+
| 2595 | [Number of Even and Odd Bits](https://leetcode.com/problems/number-of-even-and-odd-bits) | [![Java](assets/java.png)](src/NumberOfEvenAndOddBits.java) | |
812812
| 2600 | [K Items With the Maximum Sum](https://leetcode.com/problems/k-items-with-the-maximum-sum) | | |
813813
| 2605 | [Form Smallest Number From Two Digit Arrays](https://leetcode.com/problems/form-smallest-number-from-two-digit-arrays) | | |
814814
| 2605 | [Form Smallest Number From Two Digit Arrays](https://leetcode.com/problems/form-smallest-number-from-two-digit-arrays) | | |

Diff for: src/NumberOfEvenAndOddBits.java

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// https://leetcode.com/problems/number-of-even-and-odd-bits
2+
// T: O(log(N))
3+
// S: O(1)
4+
5+
public class NumberOfEvenAndOddBits {
6+
public int[] evenOddBit(int n) {
7+
return new int[] { evenBits(n), oddBits(n)};
8+
}
9+
10+
private int evenBits(int x) {
11+
int bits = 0, num = 1;
12+
while (num <= x) {
13+
bits += (x & num) > 0 ? 1 : 0;
14+
num <<= 2;
15+
}
16+
return bits;
17+
}
18+
19+
private int oddBits(int x) {
20+
int bits = 0, num = 2;
21+
while (num <= x) {
22+
bits += (x & num) > 0 ? 1 : 0;
23+
num <<= 2;
24+
}
25+
return bits;
26+
}
27+
}

0 commit comments

Comments
 (0)