Skip to content

[pull] main from doocs:main #444

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 4 commits into from
Mar 27, 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
39 changes: 38 additions & 1 deletion solution/0800-0899/0822.Card Flipping Game/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ tags:

### 方法一:哈希表

我们注意到,对于位置 $i$,若 $fronts[i]$ 与 $backs[i]$ 元素相同,则一定不满足条件。
我们注意到,对于位置 $i$,若 $\textit{fronts}[i]$ 与 $\textit{backs}[i]$ 元素相同,则一定不满足条件。

因此,我们先找出正面与背面相同的元素,记录在哈希表 $s$ 中。

Expand Down Expand Up @@ -195,6 +195,43 @@ function flipgame(fronts: number[], backs: number[]): number {
}
```

#### Rust

```rust
use std::collections::HashSet;

impl Solution {
pub fn flipgame(fronts: Vec<i32>, backs: Vec<i32>) -> i32 {
let n = fronts.len();
let mut s: HashSet<i32> = HashSet::new();

for i in 0..n {
if fronts[i] == backs[i] {
s.insert(fronts[i]);
}
}

let mut ans = 9999;
for &v in fronts.iter() {
if !s.contains(&v) {
ans = ans.min(v);
}
}
for &v in backs.iter() {
if !s.contains(&v) {
ans = ans.min(v);
}
}

if ans == 9999 {
0
} else {
ans
}
}
}
```

#### C#

```cs
Expand Down
49 changes: 48 additions & 1 deletion solution/0800-0899/0822.Card Flipping Game/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,17 @@ There are no good integers no matter how we flip the cards, so we return 0.

<!-- solution:start -->

### Solution 1
### Solution 1: Hash Table

We observe that for position $i$, if $\textit{fronts}[i]$ is equal to $\textit{backs}[i]$, then it certainly does not satisfy the condition.

Therefore, we first identify all elements that appear the same on both the front and back sides and record them in a hash set $s$.

Next, we iterate through all elements in both the front and back arrays. For any element $x$ that is **not** in the hash set $s$, we update the minimum value of the answer.

Finally, if we find any element that satisfies the condition, we return the minimum answer; otherwise, we return $0$.

The time complexity is $O(n)$ and the space complexity is $O(n)$, where $n$ is the length of the arrays.

<!-- tabs:start -->

Expand Down Expand Up @@ -180,6 +190,43 @@ function flipgame(fronts: number[], backs: number[]): number {
}
```

#### Rust

```rust
use std::collections::HashSet;

impl Solution {
pub fn flipgame(fronts: Vec<i32>, backs: Vec<i32>) -> i32 {
let n = fronts.len();
let mut s: HashSet<i32> = HashSet::new();

for i in 0..n {
if fronts[i] == backs[i] {
s.insert(fronts[i]);
}
}

let mut ans = 9999;
for &v in fronts.iter() {
if !s.contains(&v) {
ans = ans.min(v);
}
}
for &v in backs.iter() {
if !s.contains(&v) {
ans = ans.min(v);
}
}

if ans == 9999 {
0
} else {
ans
}
}
}
```

#### C#

```cs
Expand Down
32 changes: 32 additions & 0 deletions solution/0800-0899/0822.Card Flipping Game/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use std::collections::HashSet;

impl Solution {
pub fn flipgame(fronts: Vec<i32>, backs: Vec<i32>) -> i32 {
let n = fronts.len();
let mut s: HashSet<i32> = HashSet::new();

for i in 0..n {
if fronts[i] == backs[i] {
s.insert(fronts[i]);
}
}

let mut ans = 9999;
for &v in fronts.iter() {
if !s.contains(&v) {
ans = ans.min(v);
}
}
for &v in backs.iter() {
if !s.contains(&v) {
ans = ans.min(v);
}
}

if ans == 9999 {
0
} else {
ans
}
}
}
Loading