diff --git a/leetcode/601-700/0679.24-Game/README.md b/leetcode/601-700/0679.24-Game/README.md index 1d5ce235e..76d338f79 100644 --- a/leetcode/601-700/0679.24-Game/README.md +++ b/leetcode/601-700/0679.24-Game/README.md @@ -1,28 +1,38 @@ # [679.24 Game][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +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: + +- The division operator `'/'` represents real division, not integer division. + + - For example, `4 / (1 - 2 / 3) = 4 / (1 / 3) = 12`. + +- 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**. + +- 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: a = "11", b = "1" -Output: "100" +Input: cards = [4,1,8,7] +Output: true +Explanation: (8-4) * (7-1) = 24 ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -24 Game -```go ``` - +Input: cards = [1,2,1,2] +Output: false +``` ## 结语 diff --git a/leetcode/601-700/0679.24-Game/Solution.go b/leetcode/601-700/0679.24-Game/Solution.go index d115ccf5e..0c0b1725c 100644 --- a/leetcode/601-700/0679.24-Game/Solution.go +++ b/leetcode/601-700/0679.24-Game/Solution.go @@ -1,5 +1,57 @@ package Solution -func Solution(x bool) bool { - return x +import "math" + +func Solution(cards []int) bool { + nums := make([]float64, len(cards)) + for i, v := range cards { + nums[i] = float64(v) + } + return dfs(nums) +} + +// 递归函数:判断当前数字数组是否可以通过操作得到24 +func dfs(nums []float64) bool { + if len(nums) == 1 { + // 终止条件:只有一个数字,判断是否接近24 + return math.Abs(nums[0]-24) < 1e-6 + } + // 遍历所有两两组合 + for i := 0; i < len(nums); i++ { + for j := i + 1; j < len(nums); j++ { + // 取出两个数 + a, b := nums[i], nums[j] + // 剩余的数字 + rest := make([]float64, 0, len(nums)-1) + for k := 0; k < len(nums); k++ { + if k != i && k != j { + rest = append(rest, nums[k]) + } + } + + // 所有可能的运算 + operations := [][]float64{ + {a + b}, + {a - b}, + {b - a}, + {a * b}, + } + // 除法要判断除数不为零 + if math.Abs(b) > 1e-6 { + operations = append(operations, []float64{a / b}) + } + if math.Abs(a) > 1e-6 { + operations = append(operations, []float64{b / a}) + } + + // 递归 + for _, op := range operations { + nextNums := append(rest, op[0]) + if dfs(nextNums) { + return true + } + } + } + } + return false } diff --git a/leetcode/601-700/0679.24-Game/Solution_test.go b/leetcode/601-700/0679.24-Game/Solution_test.go index 14ff50eb4..0f21ee9d9 100644 --- a/leetcode/601-700/0679.24-Game/Solution_test.go +++ b/leetcode/601-700/0679.24-Game/Solution_test.go @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool + inputs []int expect bool }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{4, 1, 8, 7}, true}, + {"TestCase2", []int{1, 2, 1, 2}, false}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }