-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFizzBuzz.java
More file actions
63 lines (54 loc) · 1.53 KB
/
FizzBuzz.java
File metadata and controls
63 lines (54 loc) · 1.53 KB
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
package lintcode;
public class FizzBuzz {
public static void main(String[] args) {
// int i = 5>>1;
// System.out.println(i);
//
// int j = 5;
// System.out.println(Integer.toBinaryString(j));
// System.out.println( 5 & (5-1));
countOnes3(5);
System.out.println(Integer.toBinaryString(-1));
}
/**
* counting how many 1 as binary digit in the num
*
* 5 : 101 == two 1s
* 6 : 110 == two 1s
* 7 : 111 == three 1s
*
*
* @param num
* @return
*/
public static int countOnes3(int num){
int count = 0;
while(num!=0){
num = num & (num-1);
// counter right most 1 , for example, 101 after num & (num -1) => 101 & (100) = 100 (equals 4, remove right most 1 , then 101 became 100 )
// 4 & (4-1) => 100 & 011 => 000, remove 4 , 100 right most 1, then 100 became 000 .
count++;
}
return count;
}
public static int countOnes1(int num){
int count = 0;
while(num>0){
if((num&1) ==1){
count++;
}
num= num>>1;
}
return count;
}
public static int countOnes2(int num){
int counter = 0 ;
String temp = Integer.toBinaryString(num);
for (int i = 0; i < temp.length(); i++) {
if (temp.charAt(i) == '1'){
counter++;
}
}
return counter;
}
}