42
42
43
43
** 方法一:哈希表**
44
44
45
- 对于位置 i,若 ` fronts[i] ` 与 ` backs[i] ` 元素相同,则一定不满足条件。找其他出现在 fronts 或 backs 中的元素的最小值即可。
45
+ 我们注意到,对于位置 $i$,若 $fronts[ i] $ 与 $backs[ i] $ 元素相同,则一定不满足条件。
46
+
47
+ 因此,我们先找出正面与背面相同的元素,记录在哈希表 $s$ 中。
48
+
49
+ 接下来,遍历正面与背面的元素,若元素 $x$ 不在哈希表 $s$ 中,则更新答案的最小值。
50
+
51
+ 最后,若找到一个满足条件的元素,返回答案,否则返回 $0$。
52
+
53
+ 时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组的长度。
46
54
47
55
<!-- tabs:start -->
48
56
53
61
``` python
54
62
class Solution :
55
63
def flipgame (self , fronts : List[int ], backs : List[int ]) -> int :
56
- same = {a for a, b in zip (fronts, backs) if a == b}
57
- ans = 9999
58
- for x in chain(fronts, backs):
59
- if x not in same:
60
- ans = min (ans, x)
61
- return ans % 9999
64
+ s = {a for a, b in zip (fronts, backs) if a == b}
65
+ return min ((x for x in chain(fronts, backs) if x not in s), default = 0 )
62
66
```
63
67
64
68
### ** Java**
@@ -99,16 +103,22 @@ public:
99
103
int flipgame(vector<int >& fronts, vector<int >& backs) {
100
104
unordered_set<int > s;
101
105
int n = fronts.size();
102
- for (int i = 0; i < n; ++i)
103
- if (fronts[ i] == backs[ i] )
106
+ for (int i = 0; i < n; ++i) {
107
+ if (fronts[ i] == backs[ i] ) {
104
108
s.insert(fronts[ i] );
109
+ }
110
+ }
105
111
int ans = 9999;
106
- for (int& v : fronts)
107
- if (!s.count(v))
112
+ for (int& v : fronts) {
113
+ if (!s.count(v)) {
108
114
ans = min(ans, v);
109
- for (int& v : backs)
110
- if (!s.count(v))
115
+ }
116
+ }
117
+ for (int& v : backs) {
118
+ if (!s.count(v)) {
111
119
ans = min(ans, v);
120
+ }
121
+ }
112
122
return ans % 9999;
113
123
}
114
124
};
@@ -118,20 +128,20 @@ public:
118
128
119
129
```go
120
130
func flipgame(fronts []int, backs []int) int {
121
- s := map[int]bool {}
122
- for i, v := range fronts {
123
- if v == backs[i] {
124
- s[v ] = true
131
+ s := map[int]struct{} {}
132
+ for i, a := range fronts {
133
+ if a == backs[i] {
134
+ s[a ] = struct{}{}
125
135
}
126
136
}
127
137
ans := 9999
128
138
for _, v := range fronts {
129
- if ! s[v] {
139
+ if _, ok := s[v]; !ok {
130
140
ans = min(ans, v)
131
141
}
132
142
}
133
143
for _, v := range backs {
134
- if ! s[v] {
144
+ if _, ok := s[v]; !ok {
135
145
ans = min(ans, v)
136
146
}
137
147
}
@@ -150,26 +160,52 @@ func min(a, b int) int {
150
160
151
161
``` ts
152
162
function flipgame(fronts : number [], backs : number []): number {
153
- const s: Set <number > = new Set ();
154
- const n = fronts .length ;
155
- for (let i = 0 ; i < n ; ++ i ) {
156
- if (fronts [i ] === backs [i ]) {
157
- s .add (fronts [i ]);
158
- }
159
- }
160
- let ans = 9999 ;
161
- for (const v of fronts ) {
162
- if (! s .has (v )) {
163
- ans = Math .min (ans , v );
164
- }
165
- }
166
- for (const v of backs ) {
167
- if (! s .has (v )) {
168
- ans = Math .min (ans , v );
169
- }
170
- }
171
- return ans % 9999 ;
172
- };
163
+ const s: Set <number > = new Set ();
164
+ const n = fronts .length ;
165
+ for (let i = 0 ; i < n ; ++ i ) {
166
+ if (fronts [i ] === backs [i ]) {
167
+ s .add (fronts [i ]);
168
+ }
169
+ }
170
+ let ans = 9999 ;
171
+ for (const v of fronts ) {
172
+ if (! s .has (v )) {
173
+ ans = Math .min (ans , v );
174
+ }
175
+ }
176
+ for (const v of backs ) {
177
+ if (! s .has (v )) {
178
+ ans = Math .min (ans , v );
179
+ }
180
+ }
181
+ return ans % 9999 ;
182
+ }
183
+ ```
184
+
185
+ ### ** C#**
186
+
187
+ ``` cs
188
+ public class Solution {
189
+ public int Flipgame (int [] fronts , int [] backs ) {
190
+ var s = new HashSet <int >();
191
+ int n = fronts .Length ;
192
+ for (int i = 0 ; i < n ; ++ i ) {
193
+ if (fronts [i ] == backs [i ]) {
194
+ s .Add (fronts [i ]);
195
+ }
196
+ }
197
+ int ans = 9999 ;
198
+ for (int i = 0 ; i < n ; ++ i ) {
199
+ if (! s .Contains (fronts [i ])) {
200
+ ans = Math .Min (ans , fronts [i ]);
201
+ }
202
+ if (! s .Contains (backs [i ])) {
203
+ ans = Math .Min (ans , backs [i ]);
204
+ }
205
+ }
206
+ return ans % 9999 ;
207
+ }
208
+ }
173
209
```
174
210
175
211
### ** ...**
0 commit comments