Skip to content

Add solution and test-cases for problem 679 #1288

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions leetcode/601-700/0679.24-Game/README.md
Original file line number Diff line number Diff line change
@@ -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
```

## 结语

Expand Down
56 changes: 54 additions & 2 deletions leetcode/601-700/0679.24-Game/Solution.go
Original file line number Diff line number Diff line change
@@ -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
}
11 changes: 5 additions & 6 deletions leetcode/601-700/0679.24-Game/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
}

// 开始测试
Expand All @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading