-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryWatch.java
More file actions
40 lines (36 loc) · 1 KB
/
Copy pathBinaryWatch.java
File metadata and controls
40 lines (36 loc) · 1 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
import java.util.ArrayList;
import java.util.List;
public class BinaryWatch {
public List<String> readBinaryWatch(int num) {
int[] times = new int[]{8,4,2,1,32,16,8,4,2,1};
List<String> res = new ArrayList<String>();
read(res, new ArrayList<Integer>(), 0, num, times);
return res;
}
private void read(List<String> res, List<Integer> seq, int start, int num, int[] times){
if(seq.size() == num){
int hour = 0;
int minute = 0;
for(int i : seq){
if(i < 4)
hour += times[i];
else
minute += times[i];
}
if(hour < 12 && minute < 60){
String time = String.valueOf(hour) + ":" + (minute < 10 ? "0" + String.valueOf(minute) : String.valueOf(minute));
res.add(time);
}
}else{
for(int i = start; i < 10; i++){
seq.add(i);
read(res, seq, i + 1, num, times);
seq.remove(seq.size() - 1);
}
}
}
public static void main(String[] args) {
BinaryWatch watch = new BinaryWatch();
System.out.println(watch.readBinaryWatch(8));
}
}