classSolution: deffindMissingAndRepeatedValues(self, grid: List[List[int]]) -> List[int]: n = len(grid) cnt = [0] * (n * n + 1) for row in grid: for x in row: cnt[x] += 1 ans = [-1, -1] for i inrange(1, n * n + 1): if cnt[i] == 2: # repeated, a ans[0] = i elif cnt[i] == 0: # missing, b ans[1] = i return ans
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution { public: vector<int> findMissingAndRepeatedValues(vector<vector<int>>& grid){ int n = grid.size(); unordered_map<int, int> cnt; for (int i=0; i<n; i++) for (int j=0; j<n; j++) cnt[grid[i][j]]++; vector<int> ans(2); for (int i=1; i<=n*n; i++){ if (cnt[i] == 2) ans[0] = i; // repeated, a elseif (cnt[i] == 0) ans[1] = i; // missing, b } return ans; } };
方法二:位運算(Bit Manipulation)
將 grid 視為長度為 n2 的一維陣列,若在 grid 中額外添加 [1,n2] 的所有數字,則在 [1,n2] 中重複的數字 a 會出現三次、缺失的數字 b 會出現一次,其餘數字皆會出現兩次。
但需注意,在該題中返回的順序是任意的,而這題要求返回的順序是 [a,b],因此在最後需要檢查 a 是否在 grid 中,若不在則將 a 和 b 交換。
複雜度分析
時間複雜度 O(n2)
空間複雜度 O(1)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classSolution: deffindMissingAndRepeatedValues(self, grid: List[List[int]]) -> List[int]: n = len(grid) # 異或所有數字 xor = 0# xor of all numbers for row in grid: for x in row: xor ^= x for x inrange(1, n * n + 1): xor ^= x # 分組異或 lb = xor & -xor # 最低位的 1 ans = [0, 0] for row in grid: for x in row: ans[x & lb == 0] ^= x # 分組異或 for x inrange(1, n * n + 1): ans[x & lb == 0] ^= x # 分組異或 return ans if ans[0] in [x for row in grid for x in row] else ans[::-1]
classSolution { public: vector<int> findMissingAndRepeatedValues(vector<vector<int>>& grid){ int n = grid.size(); // xor all numbers int s = 0; // xor of all numbers for (int i=0; i<n; i++) for (int j=0; j<n; j++) s ^= grid[i][j]; for (int i=1; i<=n*n; i++) s ^= i; // group xor int lb = s & -s; // lowest bit of 1 vector<int> ans(2); for (int i=0; i<n; i++) for (int j=0; j<n; j++) ans[(grid[i][j] & lb) == 0] ^= grid[i][j]; for (int i=1; i<=n*n; i++) ans[(i & lb) == 0] ^= i; // check if ans[0] is in grid for (auto& row : grid) if (find(row.begin(), row.end(), ans[0]) != row.end()) return ans; return {ans[1], ans[0]}; } };
classSolution: deffindMissingAndRepeatedValues(self, grid: List[List[int]]) -> List[int]: n, m = len(grid), len(grid) ** 2 s1 = sum(x for row in grid for x in row) # sum of all numbers s2 = sum(x * x for row in grid for x in row) # sum of all squares d1 = s1 - m * (m + 1) // 2# a - b d2 = s2 - m * (m + 1) * (m * 2 + 1) // 6# a^2 - b^2 = (a + b)(a - b) return [(d2 // d1 + d1) // 2, (d2 // d1 - d1) // 2] # (a, b)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
using LL = longlong; classSolution { public: vector<int> findMissingAndRepeatedValues(vector<vector<int>>& grid){ int n = grid.size(); LL m = n * n, d1 = 0, d2 = 0; for (int i=0; i<n; i++) for (int j=0; j<n; j++){ d1 += grid[i][j]; // sum of all numbers d2 += grid[i][j] * grid[i][j]; // sum of all squares } d1 -= m * (m + 1) / 2; // a - b d2 -= m * (m + 1) * (m * 2 + 1) / 6; // a^2 - b^2 = (a + b)(a - b) return {(int) (d2 / d1 + d1) / 2, (int) (d2 / d1 - d1) / 2}; // (a, b) } };