-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path679-24Game.go
More file actions
138 lines (125 loc) · 4.53 KB
/
679-24Game.go
File metadata and controls
138 lines (125 loc) · 4.53 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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
// 679. 24 Game
// You are given an integer array cards of length 4.
// You have four cards, each containing a number in the range [1, 9].
// You should arrange the numbers on these cards in a mathematical expression using the operators ['+', '-', '*', '/'] and the parentheses '(' and ')' to get the value 24.
// You are restricted with the following rules:
// 1. The division operator '/' represents real division, not integer division.
// For example, 4 / (1 - 2 / 3) = 4 / (1 / 3) = 12.
// 2. Every operation done is between two numbers. In particular, we cannot use '-' as a unary operator.
// For example, if cards = [1, 1, 1, 1], the expression "-1 - 1 - 1 - 1" is not allowed.
// 3. You cannot concatenate numbers together
// For example, if cards = [1, 2, 1, 2], the expression "12 + 12" is not valid.
// Return true if you can get such expression that evaluates to 24, and false otherwise.
// Example 1:
// Input: cards = [4,1,8,7]
// Output: true
// Explanation: (8-4) * (7-1) = 24
// Example 2:
// Input: cards = [1,2,1,2]
// Output: false
// Constraints:
// cards.length == 4
// 1 <= cards[i] <= 9
import "fmt"
import "math"
func judgePoint24(cards []int) bool {
// 转换成 float64
arr := []float64{}
for _, c := range cards {
arr = append(arr, float64(c))
}
abs := func (x float64) float64 { if x < 0 { return -x }; return x }
operationsResult := func(a, b float64) []float64 { // 计算两个数的各种 + - * / 4则运算的各种可能
res := []float64{ a - b, a + b, b - a, a * b}
if a > 0 { res = append(res, b / a) }
if b > 0 { res = append(res, a / b) }
return res
}
var dfs func(cards []float64) bool
dfs = func (cards []float64) bool {
n := len(cards)
if n == 1 {
return abs(cards[0] - 24) <= 0.1
}
for i := 0; i < n; i++ {
for j := i + 1; j < n; j++ {
var list []float64
for k := 0; k < n; k++ {
if k != i && k != j {
list = append(list, cards[k])
}
}
ops := operationsResult(cards[i], cards[j])
for _, op := range ops {
list = append(list, op)
if dfs(list) { // Recur
return true
}
list = list[:len(list)-1]
}
}
}
return false
}
return dfs(arr)
}
func judgePoint241(cards []int) bool {
flCards := []float64{}
for _, v := range cards {
flCards = append(flCards, float64(v))
}
generateAllPossibleResults := func(a, b float64) []float64 {
res := []float64{ a - b, b - a, a + b, a * b, }
if a != 0 {
res = append(res, b / a)
}
if b != 0 {
res = append(res, a / b)
}
return res
}
var checkIfResultIsReached func(nums []float64) bool
checkIfResultIsReached = func(nums []float64) bool {
if len(nums) == 1 {
return math.Abs(float64(nums[0]) - 24) <= 0.1
}
for i := 0; i < len(nums); i++ {
for j := i + 1; j < len(nums); j++ {
var newNums []float64
for k := 0; k < len(nums); k++ {
if k != i && k != j {
newNums = append(newNums, nums[k])
}
}
possibleResults := generateAllPossibleResults(nums[i], nums[j])
for _, res := range possibleResults {
newNums = append(newNums, res)
if checkIfResultIsReached(newNums) {
return true
}
newNums = newNums[:len(newNums)-1]
}
}
}
return false
}
return checkIfResultIsReached(flCards)
}
func main() {
// Example 1:
// Input: cards = [4,1,8,7]
// Output: true
// Explanation: (8-4) * (7-1) = 24
fmt.Println(judgePoint24([]int{4,1,8,7})) // true
// Example 2:
// Input: cards = [1,2,1,2]
// Output: false
fmt.Println(judgePoint24([]int{1,2,1,2})) // false
fmt.Println(judgePoint24([]int{1,2,3,4})) // true
fmt.Println(judgePoint24([]int{4,3,2,1})) // false
fmt.Println(judgePoint241([]int{4,1,8,7})) // true
fmt.Println(judgePoint241([]int{1,2,1,2})) // false
fmt.Println(judgePoint241([]int{1,2,3,4})) // true
fmt.Println(judgePoint241([]int{4,3,2,1})) // false
}