-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryWatch0401.java
More file actions
59 lines (49 loc) · 1.37 KB
/
Copy pathBinaryWatch0401.java
File metadata and controls
59 lines (49 loc) · 1.37 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
import java.util.ArrayList;
import java.util.List;
/**
* ¶þ½øÖÆÊÖ±í
*/
public class BinaryWatch0401{
public static void main(String[] args) {
readBinaryWatch(2);
}
// --------------
public static int hm[] = {1, 2, 4, 8, 1, 2, 4, 8, 16, 32};
public static int flag[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
public static List<String> result = new ArrayList<>();
public static List<String> readBinaryWatch(int num) {
result = new ArrayList<>();
backtrack(0, num);
for (String s : result) {
System.out.println(s);
}
return result;
}
private static void backtrack(int start, int num) {
if (num <= 0) {
int h = 0;
int m = 0;
for (int i = 0; i < hm.length; i++) {
if (flag[i] != 0) {
continue;
}
if (i < 4) {
h += hm[i];
}else {
m += hm[i];
}
}
if (h < 12 && m < 60){
result.add(String.format("%d:%02d", h, m));
}
return;
}
for (int i = start; i < hm.length; i++) {
if (flag[i] == 1) {
flag[i] = 0;
backtrack(i+1, num-1);
flag[i] = 1;
}
}
}
}