-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path679.24Game.h
More file actions
67 lines (53 loc) · 2.04 KB
/
679.24Game.h
File metadata and controls
67 lines (53 loc) · 2.04 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
60
61
62
63
64
65
66
67
/*
You have 4 cards each containing a number from 1 to 9. You need to judge whether they could operated through *, /, +, -, (, ) to get the value of 24.
Example 1:
Input: [4, 1, 8, 7]
Output: True
Explanation: (8-4) * (7-1) = 24
Example 2:
Input: [1, 2, 1, 2]
Output: False
Note:
The division operator / represents real division, not integer division. For example, 4 / (1 - 2/3) = 12.
Every operation done is between two numbers. In particular, we cannot use - as a unary operator. For example, with [1, 1, 1, 1] as input, the expression -1 - 1 - 1 - 1 is not allowed.
You cannot concatenate numbers together. For example, if the input is [1, 2, 1, 2], we cannot write this as 12 + 12.
*/
bool judgePoint24(vector<int>& nums) {
bool ret = false;
double eps = 0.001;
vector<double> arr(nums.begin(), nums.end());
helper(arr, eps, ret);
return ret;
}
void helper(vector<double> nums, double eps, bool &ret) {
if (ret) {
return;
}
if (nums.size() == 1) {
if (abs(nums[0]-24) < eps) {
ret = true;
return;
}
}
for (int i = 0; i < nums.size(); i++) {
for (int j = 0; j < i; j++) {
double p = nums[i], q = nums[j];
vector<double> t {p+q, p-q, q-p, p*q};
if (p > eps) {
t.push_back(q/p);
}
if (q > eps) {
t.push_back(p/q);
}
nums.erase(nums.begin()+i);
nums.erase(nums.begin()+j);
for (double d : t) {
nums.push_back(t);
helper(nums, eps, ret);
nums.pop_back();
}
nums.insert(nums.begin()+j, q);
nums.insert(nums.begin()+i, p);
}
}
}