Skip to content

Commit 1ba35fe

Browse files
committed
一刷822
1 parent 8503840 commit 1ba35fe

6 files changed

Lines changed: 103 additions & 29 deletions

File tree

README.adoc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5779,13 +5779,13 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
57795779
|Easy
57805780
|
57815781

5782-
//|{counter:codes}
5783-
//|{leetcode_base_url}/card-flipping-game/[822. Card Flipping Game^]
5784-
//|{source_base_url}/_0822_CardFlippingGame.java[Java]
5785-
//|{doc_base_url}/0822-card-flipping-game.adoc[题解]
5786-
//|Medium
5787-
//|
5788-
//
5782+
|{counter:codes}
5783+
|{leetcode_base_url}/card-flipping-game/[822. Card Flipping Game^]
5784+
|{source_base_url}/_0822_CardFlippingGame.java[Java]
5785+
|{doc_base_url}/0822-card-flipping-game.adoc[题解]
5786+
|Medium
5787+
|
5788+
57895789
//|{counter:codes}
57905790
//|{leetcode_base_url}/binary-trees-with-factors/[823. Binary Trees With Factors^]
57915791
//|{source_base_url}/_0823_BinaryTreesWithFactors.java[Java]

docs/0822-card-flipping-game.adoc

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,74 @@
11
[#0822-card-flipping-game]
2-
= 822. Card Flipping Game
2+
= 822. 翻转卡片游戏
33

4-
{leetcode}/problems/card-flipping-game/[LeetCode - Card Flipping Game^]
4+
https://leetcode.cn/problems/card-flipping-game/[LeetCode - 822. 翻转卡片游戏^]
55

6-
On a table are `N` cards, with a positive integer printed on the front and back of each card (possibly different).
6+
在桌子上有 `n` 张卡片,每张卡片的正面和背面都写着一个正数(正面与背面上的数有可能不一样)。
77

8-
We flip any number of cards, and after we choose one card.
8+
我们可以先翻转任意张卡片,然后选择其中一张卡片。
99

10-
If the number `X` on the back of the chosen card is not on the front of any card, then this number X is good.
10+
如果选中的那张卡片背面的数字 `x` 与任意一张卡片的正面的数字都不同,那么这个数字是我们想要的数字。
1111

12-
What is the smallest number that is good? If no number is good, output `0`.
12+
哪个数是这些想要的数字中最小的数(找到这些数中的最小值)呢?如果没有一个数字符合要求的,输出 `0`
1313

14-
Here, `fronts[i]` and `backs[i]` represent the number on the front and back of card `i`.
14+
其中, `fronts[i]` `backs[i]` 分别代表第 `i` 张卡片的正面和背面的数字。
1515

16-
A flip swaps the front and back numbers, so the value on the front is now on the back and vice versa.
16+
如果我们通过翻转卡片来交换正面与背面上的数,那么当初在正面的数就变成背面的数,背面的数就变成正面的数。
1717

18-
*Example:*
18+
*示例 1:*
1919

20-
[subs="verbatim,quotes,macros"]
21-
----
22-
*Input:* fronts = [1,2,4,4,7], backs = [1,3,4,1,3]
23-
*Output:* `2`
24-
*Explanation:* If we flip the second card, the fronts are `[1,3,4,4,7]` and the backs are `[1,2,4,1,3]`.
25-
We choose the second card, which has number 2 on the back, and it isn't on the front of any card, so `2` is good.
26-
----
20+
....
21+
输入:fronts = [1,2,4,4,7], backs = [1,3,4,1,3]
22+
输出:2
23+
解释:假设我们翻转第二张卡片,那么在正面的数变成了 [1,3,4,4,7] , 背面的数变成了 [1,2,4,1,3]。
24+
接着我们选择第二张卡片,因为现在该卡片的背面的数是 2,2 与任意卡片上正面的数都不同,所以 2 就是我们想要的数字。
25+
....
26+
27+
*示例 2:*
2728

28-
29+
....
30+
输入:fronts = [1], backs = [1]
31+
输出:0
32+
解释:
33+
无论如何翻转都无法得到想要的数字,所以返回 0 。
34+
....
2935

30-
*Note:*
36+
*提示:*
3137

38+
* `n == fronts.length == backs.length`
39+
* `1 \<= n \<= 1000`
40+
* `1 \<= fronts[i], backs[i] \<= 2000`
3241
33-
. `1 <= fronts.length == backs.length <= 1000`.
34-
. `1 <= fronts[i] <= 2000`.
35-
. `1 <= backs[i] <= 2000`.
3642
3743
44+
== 思路分析
3845

46+
首先,排除的是正反面一样的数字。剩下的数字里,找最小的数字即可。有点想复杂了。
3947

4048
[[src-0822]]
49+
[tabs]
50+
====
51+
一刷::
52+
+
53+
--
4154
[{java_src_attr}]
4255
----
4356
include::{sourcedir}/_0822_CardFlippingGame.java[tag=answer]
4457
----
58+
--
59+
60+
// 二刷::
61+
// +
62+
// --
63+
// [{java_src_attr}]
64+
// ----
65+
// include::{sourcedir}/_0822_CardFlippingGame_2.java[tag=answer]
66+
// ----
67+
// --
68+
====
69+
70+
71+
== 参考资料
4572

73+
. https://leetcode.cn/problems/card-flipping-game/solutions/2368863/yue-du-li-jie-ti-pythonjavacgojs-by-endl-ze7f/[822. 翻转卡片游戏 - 阅读理解题^]
74+
. https://leetcode.cn/problems/card-flipping-game/solutions/2365854/fan-zhuan-qia-pian-you-xi-by-leetcode-so-acbj/[822. 翻转卡片游戏 - 官方题解^]

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1736,7 +1736,7 @@ include::0820-short-encoding-of-words.adoc[leveloffset=+1]
17361736

17371737
include::0821-shortest-distance-to-a-character.adoc[leveloffset=+1]
17381738

1739-
// include::0822-card-flipping-game.adoc[leveloffset=+1]
1739+
include::0822-card-flipping-game.adoc[leveloffset=+1]
17401740

17411741
// include::0823-binary-trees-with-factors.adoc[leveloffset=+1]
17421742

logbook/202601.adoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,12 @@ endif::[]
10211021
|{doc_base_url}/0820-short-encoding-of-words.adoc[题解]
10221022
|✅ 排序。按长度倒序,后根据判断是否存在在决定是否插入。更优解是前缀树。
10231023

1024+
|{counter:codes}
1025+
|{leetcode_base_url}/card-flipping-game/[822. 翻转卡片游戏^]
1026+
|{doc_base_url}/0822-card-flipping-game.adoc[题解]
1027+
|❌ 哈希。想复杂了。排除的是正反面一样的数字。剩下的数字里,找最小的数字即可。
1028+
1029+
10241030

10251031
|===
10261032

logbook/corrections.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,6 +1233,11 @@ endif::[]
12331233
|{doc_base_url}/0821-shortest-distance-to-a-character.adoc[题解]
12341234
|❌
12351235

1236+
|{counter:codes}
1237+
|{leetcode_base_url}/card-flipping-game/[822. 翻转卡片游戏^]
1238+
|{doc_base_url}/0822-card-flipping-game.adoc[题解]
1239+
|❌
1240+
12361241
|===
12371242

12381243
截止目前,本轮练习一共完成 {corrections} 道题。
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
public class _0822_CardFlippingGame {
7+
// tag::answer[]
8+
9+
/**
10+
* @author D瓜哥 · https://www.diguage.com
11+
* @since 2026-07-15 22:52:49
12+
*/
13+
public int flipgame(int[] fronts, int[] backs) {
14+
Set<Integer> set = new HashSet<>();
15+
for (int i = 0; i < fronts.length; i++) {
16+
if (fronts[i] == backs[i]) {
17+
set.add(fronts[i]);
18+
}
19+
}
20+
int result = Integer.MAX_VALUE;
21+
for (int front : fronts) {
22+
if (!set.contains(front)) {
23+
result = Math.min(result, front);
24+
}
25+
}
26+
for (int back : backs) {
27+
if (!set.contains(back)) {
28+
result = Math.min(result, back);
29+
}
30+
}
31+
return result == Integer.MAX_VALUE ? 0 : result;
32+
}
33+
// end::answer[]
34+
}

0 commit comments

Comments
 (0)