From dd52f133cad2b26fbb667faa81a8b01d7e247d77 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Mon, 18 Aug 2025 07:57:54 +0800 Subject: [PATCH] feat: add rust solution to lc problem: No.0679 No.0679.24 Game --- solution/0600-0699/0679.24 Game/README.md | 56 ++++++++++++++++++++ solution/0600-0699/0679.24 Game/README_EN.md | 56 ++++++++++++++++++++ solution/0600-0699/0679.24 Game/Solution.rs | 51 ++++++++++++++++++ 3 files changed, 163 insertions(+) create mode 100644 solution/0600-0699/0679.24 Game/Solution.rs diff --git a/solution/0600-0699/0679.24 Game/README.md b/solution/0600-0699/0679.24 Game/README.md index 35866f6441268..739faf0301fff 100644 --- a/solution/0600-0699/0679.24 Game/README.md +++ b/solution/0600-0699/0679.24 Game/README.md @@ -358,6 +358,62 @@ function judgePoint24(cards: number[]): boolean { } ``` +#### Rust + +```rust +impl Solution { + pub fn judge_point24(cards: Vec) -> bool { + fn dfs(nums: Vec) -> bool { + let n = nums.len(); + if n == 1 { + return (nums[0] - 24.0).abs() < 1e-6; + } + for i in 0..n { + for j in 0..n { + if i == j { + continue; + } + let mut nxt = Vec::new(); + for k in 0..n { + if k != i && k != j { + nxt.push(nums[k]); + } + } + for op in 0..4 { + let mut nxt2 = nxt.clone(); + match op { + 0 => { + nxt2.push(nums[i] + nums[j]); + } + 1 => { + nxt2.push(nums[i] - nums[j]); + } + 2 => { + nxt2.push(nums[i] * nums[j]); + } + 3 => { + if nums[j].abs() < 1e-6 { + continue; + } + nxt2.push(nums[i] / nums[j]); + } + _ => {} + } + if dfs(nxt2) { + return true; + } + } + } + } + false + } + + let nums: Vec = cards.into_iter().map(|x| x as f64).collect(); + dfs(nums) + } +} +``` + diff --git a/solution/0600-0699/0679.24 Game/README_EN.md b/solution/0600-0699/0679.24 Game/README_EN.md index ec8201e1b0381..7dab19bc8a1d4 100644 --- a/solution/0600-0699/0679.24 Game/README_EN.md +++ b/solution/0600-0699/0679.24 Game/README_EN.md @@ -356,6 +356,62 @@ function judgePoint24(cards: number[]): boolean { } ``` +#### Rust + +```rust +impl Solution { + pub fn judge_point24(cards: Vec) -> bool { + fn dfs(nums: Vec) -> bool { + let n = nums.len(); + if n == 1 { + return (nums[0] - 24.0).abs() < 1e-6; + } + for i in 0..n { + for j in 0..n { + if i == j { + continue; + } + let mut nxt = Vec::new(); + for k in 0..n { + if k != i && k != j { + nxt.push(nums[k]); + } + } + for op in 0..4 { + let mut nxt2 = nxt.clone(); + match op { + 0 => { + nxt2.push(nums[i] + nums[j]); + } + 1 => { + nxt2.push(nums[i] - nums[j]); + } + 2 => { + nxt2.push(nums[i] * nums[j]); + } + 3 => { + if nums[j].abs() < 1e-6 { + continue; + } + nxt2.push(nums[i] / nums[j]); + } + _ => {} + } + if dfs(nxt2) { + return true; + } + } + } + } + false + } + + let nums: Vec = cards.into_iter().map(|x| x as f64).collect(); + dfs(nums) + } +} +``` + diff --git a/solution/0600-0699/0679.24 Game/Solution.rs b/solution/0600-0699/0679.24 Game/Solution.rs new file mode 100644 index 0000000000000..b9381fcbb552f --- /dev/null +++ b/solution/0600-0699/0679.24 Game/Solution.rs @@ -0,0 +1,51 @@ +impl Solution { + pub fn judge_point24(cards: Vec) -> bool { + fn dfs(nums: Vec) -> bool { + let n = nums.len(); + if n == 1 { + return (nums[0] - 24.0).abs() < 1e-6; + } + for i in 0..n { + for j in 0..n { + if i == j { + continue; + } + let mut nxt = Vec::new(); + for k in 0..n { + if k != i && k != j { + nxt.push(nums[k]); + } + } + for op in 0..4 { + let mut nxt2 = nxt.clone(); + match op { + 0 => { + nxt2.push(nums[i] + nums[j]); + } + 1 => { + nxt2.push(nums[i] - nums[j]); + } + 2 => { + nxt2.push(nums[i] * nums[j]); + } + 3 => { + if nums[j].abs() < 1e-6 { + continue; + } + nxt2.push(nums[i] / nums[j]); + } + _ => {} + } + if dfs(nxt2) { + return true; + } + } + } + } + false + } + + let nums: Vec = cards.into_iter().map(|x| x as f64).collect(); + dfs(nums) + } +}