classSolution { public: intflipgame(vector<int>& fronts, vector<int>& backs){ int res = 3000, n = fronts.size(); unordered_set<int> same; for (int i = 0; i < n; ++i) { if (fronts[i] == backs[i]) { same.insert(fronts[i]); } } for (int &x : fronts) { if (x < res && same.count(x) == 0) { res = x; } } for (int &x : backs) { if (x < res && same.count(x) == 0) { res = x; } } return res % 3000; } };
classSolution { publicintflipgame(int[] fronts, int[] backs) { Set<Integer> same = newHashSet(); for (inti=0; i < fronts.length; ++i) { if (fronts[i] == backs[i]) { same.add(fronts[i]); } } intres=3000; for (int x : fronts) { if (x < res && !same.contains(x)){ res = x; } } for (int x : backs) { if (x < res && !same.contains(x)) { res = x; } } return res % 3000; } }
[sol1-Python3]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classSolution: defflipgame(self, fronts: List[int], backs: List[int]) -> int: n = len(fronts) same = set() for i inrange(n): if fronts[i] == backs[i]: same.add(fronts[i]) res = 3000 for a in fronts: if a < res and a notin same: res = a for a in backs: if a < res and a notin same: res = a return res if res < 3000else0
funcflipgame(fronts, backs []int)int { same := make(map[int]bool) for i := range fronts { if fronts[i] == backs[i] { same[fronts[i]] = true } } res := 3000 for _, x := range fronts { if x < res && !same[x] { res = x } } for _, x := range backs { if x < res && !same[x] { res = x } } return res % 3000 }
var flipgame = function(fronts, backs) { const same = newSet(); for (let i = 0; i < fronts.length; i++) { if (fronts[i] === backs[i]) { same.add(fronts[i]); } } let res = 3000; for (let x of fronts) { if (x < res && !same.has(x)) { res = x; } } for (let x of backs) { if (x < res && !same.has(x)) { res = x; } } return res % 3000; };
publicclassSolution { publicintFlipgame(int[] fronts, int[] backs) { var same = new HashSet<int>(); for (int i = 0; i < fronts.Length; i++) { if (fronts[i] == backs[i]) { same.Add(fronts[i]); } } int res = 3000; foreach (var x in fronts) { if (x < res && !same.Contains(x)) { res = x; } } foreach (var x in backs) { if (x < res && !same.Contains(x)) { res = x; } } return res % 3000; } }
[sol1-C]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
intflipgame(int* fronts, int frontsSize, int* backs, int backsSize){ int res = 3000, n = backsSize; bool same[2001]; memset(same, false, sizeof(same)); for (int i = 0; i < n; i++) { if (fronts[i] == backs[i]) { same[fronts[i]] = true; } } for (int i = 0; i < n; ++i) { if (fronts[i] < res && !same[fronts[i]]) { res = fronts[i]; } if (backs[i] < res && !same[backs[i]]) { res = backs[i]; } } return res % 3000; }