-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathsolution.java
More file actions
27 lines (20 loc) · 797 Bytes
/
solution.java
File metadata and controls
27 lines (20 loc) · 797 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
class Solution {
public List<String> readBinaryWatch(int turnedOn) {
List<String> result = new ArrayList<>();
// Try all possible hours
for (int hour = 0; hour < 12; hour++) {
// Try all possible minutes
for (int minute = 0; minute < 60; minute++) {
// Count set bits of hour and minute
int totalBits = Integer.bitCount(hour) + Integer.bitCount(minute);
if (totalBits == turnedOn) {
// Format minute with leading zero
String time = hour + ":" +
(minute < 10 ? "0" + minute : minute);
result.add(time);
}
}
}
return result;
}
}