-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP16_BinaryWatch.java
More file actions
34 lines (28 loc) · 805 Bytes
/
P16_BinaryWatch.java
File metadata and controls
34 lines (28 loc) · 805 Bytes
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
package LeetCode.Dec2019;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class P16_BinaryWatch {
public static void main(String args[]){
System.out.println(readBinaryWatch(1));
}
public static ArrayList<String> readBinaryWatch(int num) {
ArrayList<String> ans = new ArrayList<>();
for (int i=0 ; i<12 ; i++) {
for (int j = 0; j < 60; j++){
if (bitCount(i) + bitCount(j) == num)
ans.add(String.format("%d:%02d", i, j));
}
}
return ans;
}
private static int bitCount(int num) {
int result = 0;
while (num > 0) {
if (num%2 == 1)
result++;
num /= 2;
}
return result;
}
}