-
Notifications
You must be signed in to change notification settings - Fork 23
/
Count 1 in Binary.java
executable file
·72 lines (57 loc) · 1.38 KB
/
Count 1 in Binary.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
E
1522133098
tags: Bit Manipulation
count 一个 32-bit number binary format 里面有多少1
#### Bit Manipulation
- shift >> i
- apply mask & 1
#### Convert to string O(n) space
可以把integer -> string -> char array.
```
/*
LintCode
Count how many 1 in binary representation of a 32-bit integer.
Example
Given 32, return 1
Given 5, return 2
Given 1023, return 9
Challenge
If the integer is n bits with m 1 bits. Can you do it in O(m) time?
Tags Expand
Binary Bit Manipulation
Thoughts:
1. break string into char[]
2. convert char[] into integer using Character.getNumericValue()
*/
/*
Thoughts:
Shift the 32 bit integer and apply mask 1
*/
public class Solution {
public int countOnes(int num) {
int count = 0;
for (int i = 1; i <= 32; i++) {
count += num >> i & 1;
}
return count;
}
};
public class Solution {
/**
* @param num: an integer
* @return: an integer, the number of ones in num
*/
public int countOnes(int num) {
if (num < 0) {
return 0;
}
String bits = Integer.toBinaryString(num);
char[] bitArray = bits.toCharArray();
int sum = 0;
for (int i = 0; i < bitArray.length; i++) {
sum += Character.getNumericValue(bitArray[i]);
}
return sum;
}
};
```