Skip to content

Commit b3a83cc

Browse files
solves binary watch
1 parent 2264a14 commit b3a83cc

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

src/BinaryWatch.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
public class BinaryWatch {
5+
public List<String> readBinaryWatch(int num) {
6+
List<String> result = new ArrayList<String>();
7+
8+
for(int i = 0 ; i < 12 ; i++){
9+
for(int j=0 ; j < 60 ; j++){
10+
int total = countDigits(i) + countDigits(j);
11+
if(total == num){
12+
StringBuilder time = new StringBuilder();
13+
time.append(i)
14+
.append(":")
15+
.append(j < 10 ? 0 : "")
16+
.append(j);
17+
18+
result.add(time.toString());
19+
}
20+
}
21+
}
22+
23+
return result;
24+
}
25+
26+
public int countDigits(int num){
27+
int result = 0;
28+
29+
while(num > 0){
30+
if((num & 1) == 1){
31+
result++;
32+
}
33+
34+
num >>= 1;
35+
}
36+
37+
return result;
38+
}
39+
}

0 commit comments

Comments
 (0)