forked from terrytong0876/LintCode-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
104 lines (77 loc) · 2.17 KB
/
Solution.java
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import java.io.*;
import java.util.*;
/*
* To execute Java, please define "static void main" on a class
* named Solution.
*
* If you need more classes, simply define them inline.
*/
//
class Solution {
public static ArrayList<String> validateClock() {
//p:1, n:5, d: 10
int[] nums = {1,1,1,1,5,5,5,5,10,10,10,10};
ArrayList<String> rst = new ArrayList<String>();
ArrayList<Integer> list = new ArrayList<Integer>();
helper(rst, " ", 4,4,4);
return rst;
}
public static void helper(ArrayList<String> rst, ArrayList<Integer> list, int[] nums, int p, int n, int d) {
if (p < 0 || n < 0 || d < 0) {
return;
}
if (list.size() == nums.length) {
if (validate(list)) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < list.size(); i++) {
if (list.get(i) == 1) sb.append("P");
if (list.get(i) == 5) sb.append("N");
if (list.get(i) == 10) sb.append("D");
}
rst.add(sb.toString());
}
return;
}
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 1 && p > 0) {
list.add(nums[i]);
helper(rst, list, nums, p - 1, n, d);
}
else if (nums[i] == 5 && n > 0) {
list.add(nums[i]);
helper(rst, list, nums, p, n - 1, d);
}
else if (nums[i] == 10 && n > 0) {
list.add(nums[i]);
helper(rst, list, nums, p, n, d - 1);
}
list.remove(list.size() - 1);
}
}
public static boolean validate(String str) {//{p}, 0, c =
if (str == null || str.length() < 12) {
return false;
}
String[] arr = str.split(" ");
String[] test = {" "," "," "," "," "," "," "," "," "," "," "," "};
for (int i = 0; i < arr.length; i++) {
int num = Integer.parseInt(arr[i]);
if (num + i >= 12) {
int index = (num + i) % 12;
if (!test[index].equals(" ")) {
return false;
} else {
test[i] = arr[i];
}
}
}
return true;
}
public static void main(String[] args) {
ArrayList<String> rst = validateClock();
for (String string : rst) {
System.out.println(string);
}
}
}
///Generate all possible solutions, then validate them all.