Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b7fad5e

Browse files
feat: add rust solution to lc problem: No.0679 (#4654)
No.0679.24 Game
1 parent 535b4fd commit b7fad5e

File tree

3 files changed

+163
-0
lines changed

3 files changed

+163
-0
lines changed

‎solution/0600-0699/0679.24 Game/README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,62 @@ function judgePoint24(cards: number[]): boolean {
358358
}
359359
```
360360

361+
#### Rust
362+
363+
```rust
364+
impl Solution {
365+
pub fn judge_point24(cards: Vec<i32>) -> bool {
366+
fn dfs(nums: Vec<f64>) -> bool {
367+
let n = nums.len();
368+
if n == 1 {
369+
return (nums[0] - 24.0).abs() < 1e-6;
370+
}
371+
for i in 0..n {
372+
for j in 0..n {
373+
if i == j {
374+
continue;
375+
}
376+
let mut nxt = Vec::new();
377+
for k in 0..n {
378+
if k != i && k != j {
379+
nxt.push(nums[k]);
380+
}
381+
}
382+
for op in 0..4 {
383+
let mut nxt2 = nxt.clone();
384+
match op {
385+
0 => {
386+
nxt2.push(nums[i] + nums[j]);
387+
}
388+
1 => {
389+
nxt2.push(nums[i] - nums[j]);
390+
}
391+
2 => {
392+
nxt2.push(nums[i] * nums[j]);
393+
}
394+
3 => {
395+
if nums[j].abs() < 1e-6 {
396+
continue;
397+
}
398+
nxt2.push(nums[i] / nums[j]);
399+
}
400+
_ => {}
401+
}
402+
if dfs(nxt2) {
403+
return true;
404+
}
405+
}
406+
}
407+
}
408+
false
409+
}
410+
411+
let nums: Vec<f64> = cards.into_iter().map(|x| x as f64).collect();
412+
dfs(nums)
413+
}
414+
}
415+
```
416+
361417
<!-- tabs:end -->
362418

363419
<!-- solution:end -->

‎solution/0600-0699/0679.24 Game/README_EN.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,62 @@ function judgePoint24(cards: number[]): boolean {
356356
}
357357
```
358358

359+
#### Rust
360+
361+
```rust
362+
impl Solution {
363+
pub fn judge_point24(cards: Vec<i32>) -> bool {
364+
fn dfs(nums: Vec<f64>) -> bool {
365+
let n = nums.len();
366+
if n == 1 {
367+
return (nums[0] - 24.0).abs() < 1e-6;
368+
}
369+
for i in 0..n {
370+
for j in 0..n {
371+
if i == j {
372+
continue;
373+
}
374+
let mut nxt = Vec::new();
375+
for k in 0..n {
376+
if k != i && k != j {
377+
nxt.push(nums[k]);
378+
}
379+
}
380+
for op in 0..4 {
381+
let mut nxt2 = nxt.clone();
382+
match op {
383+
0 => {
384+
nxt2.push(nums[i] + nums[j]);
385+
}
386+
1 => {
387+
nxt2.push(nums[i] - nums[j]);
388+
}
389+
2 => {
390+
nxt2.push(nums[i] * nums[j]);
391+
}
392+
3 => {
393+
if nums[j].abs() < 1e-6 {
394+
continue;
395+
}
396+
nxt2.push(nums[i] / nums[j]);
397+
}
398+
_ => {}
399+
}
400+
if dfs(nxt2) {
401+
return true;
402+
}
403+
}
404+
}
405+
}
406+
false
407+
}
408+
409+
let nums: Vec<f64> = cards.into_iter().map(|x| x as f64).collect();
410+
dfs(nums)
411+
}
412+
}
413+
```
414+
359415
<!-- tabs:end -->
360416

361417
<!-- solution:end -->
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
impl Solution {
2+
pub fn judge_point24(cards: Vec<i32>) -> bool {
3+
fn dfs(nums: Vec<f64>) -> bool {
4+
let n = nums.len();
5+
if n == 1 {
6+
return (nums[0] - 24.0).abs() < 1e-6;
7+
}
8+
for i in 0..n {
9+
for j in 0..n {
10+
if i == j {
11+
continue;
12+
}
13+
let mut nxt = Vec::new();
14+
for k in 0..n {
15+
if k != i && k != j {
16+
nxt.push(nums[k]);
17+
}
18+
}
19+
for op in 0..4 {
20+
let mut nxt2 = nxt.clone();
21+
match op {
22+
0 => {
23+
nxt2.push(nums[i] + nums[j]);
24+
}
25+
1 => {
26+
nxt2.push(nums[i] - nums[j]);
27+
}
28+
2 => {
29+
nxt2.push(nums[i] * nums[j]);
30+
}
31+
3 => {
32+
if nums[j].abs() < 1e-6 {
33+
continue;
34+
}
35+
nxt2.push(nums[i] / nums[j]);
36+
}
37+
_ => {}
38+
}
39+
if dfs(nxt2) {
40+
return true;
41+
}
42+
}
43+
}
44+
}
45+
false
46+
}
47+
48+
let nums: Vec<f64> = cards.into_iter().map(|x| x as f64).collect();
49+
dfs(nums)
50+
}
51+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /