File tree Expand file tree Collapse file tree 2 files changed +80
-0
lines changed Expand file tree Collapse file tree 2 files changed +80
-0
lines changed Original file line number Diff line number Diff line change @@ -127,6 +127,10 @@ Leetcode刷题记录
127
127
+ No.51 [ cpp] ( 剑指offer/cpp/51.cpp ) [ python] ( 剑指offer/python/51.py )
128
128
+ No.59 [ cpp] ( 剑指offer/cpp/59.cpp )
129
129
130
+ ### 暴力破解
131
+
132
+ + No. 679 [ cpp] ( cpp/679.cpp )
133
+
130
134
## 专题
131
135
132
136
### 状态DP
Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+ using namespace std ;
3
+
4
+
5
+ class Solution {
6
+ public:
7
+ static constexpr int TARGET = 24 ;
8
+ static constexpr double EPSILON = 1e-6 ;
9
+ enum {ADD = 0 , MULTIPLY = 1 , SUBTRACT = 2 , DIVIDE = 3 };
10
+
11
+ bool judgePoint24 (vector<int > &nums) {
12
+ vector<double > l;
13
+ for (const int &num : nums) {
14
+ l.emplace_back (static_cast <double >(num));
15
+ }
16
+ return solve (l);
17
+ }
18
+
19
+ bool solve (vector<double > &l) {
20
+ if (l.size () == 0 ) {
21
+ return false ;
22
+ }
23
+ if (l.size () == 1 ) {
24
+ return fabs (l[0 ] - TARGET) < EPSILON;
25
+ }
26
+ int size = l.size ();
27
+ for (int i = 0 ; i < size; i++) {
28
+ for (int j = 0 ; j < size; j++) {
29
+ if (i != j) {
30
+ vector<double > list2 = vector<double >();
31
+ for (int k = 0 ; k < size; k++) {
32
+ if (k != i && k != j) {
33
+ list2.emplace_back (l[k]);
34
+ }
35
+ }
36
+ for (int k = 0 ; k < 4 ; k++) {
37
+ if (k < 2 && i > j) {
38
+ continue ;
39
+ }
40
+ switch (k)
41
+ {
42
+ case ADD:
43
+ list2.emplace_back (l[i] + l[j]);
44
+ break ;
45
+ case MULTIPLY:
46
+ list2.emplace_back (l[i] * l[j]);
47
+ break ;
48
+ case SUBTRACT:
49
+ list2.emplace_back (l[i] - l[j]);
50
+ break ;
51
+ case DIVIDE:
52
+ if (fabs (l[j]) < EPSILON) {
53
+ continue ;
54
+ }
55
+ list2.emplace_back (l[i] / l[j]);
56
+ break ;
57
+ }
58
+
59
+ if (solve (list2)) {
60
+ return true ;
61
+ }
62
+ list2.pop_back (); // 全排列
63
+ }
64
+ }
65
+ }
66
+ }
67
+ return false ;
68
+ }
69
+ };
70
+
71
+
72
+ int main (int argc, char const *argv[])
73
+ {
74
+
75
+ return 0 ;
76
+ }
You can’t perform that action at this time.
0 commit comments