|
38 | 38 |
|
39 | 39 | ## Solutions
|
40 | 40 |
|
| 41 | +**Solution 1: Counting** |
| 42 | + |
| 43 | +We create an array $cnt$ of length $n^2 + 1$ to count the frequency of each number in the matrix. |
| 44 | + |
| 45 | +Next, we traverse $i \in [1, n^2]$. If $cnt[i] = 2$, then $i$ is the duplicated number, and we set the first element of the answer to $i$. If $cnt[i] = 0$, then $i$ is the missing number, and we set the second element of the answer to $i$. |
| 46 | + |
| 47 | +The time complexity is $O(n^2)$, and the space complexity is $O(n^2)$. Here, $n$ is the side length of the matrix. |
| 48 | + |
41 | 49 | <!-- tabs:start -->
|
42 | 50 |
|
43 | 51 | ### **Python3**
|
44 | 52 |
|
45 | 53 | ```python
|
46 |
| - |
| 54 | +class Solution: |
| 55 | + def findMissingAndRepeatedValues(self, grid: List[List[int]]) -> List[int]: |
| 56 | + n = len(grid) |
| 57 | + cnt = [0] * (n * n + 1) |
| 58 | + for row in grid: |
| 59 | + for v in row: |
| 60 | + cnt[v] += 1 |
| 61 | + ans = [0] * 2 |
| 62 | + for i in range(1, n * n + 1): |
| 63 | + if cnt[i] == 2: |
| 64 | + ans[0] = i |
| 65 | + if cnt[i] == 0: |
| 66 | + ans[1] = i |
| 67 | + return ans |
47 | 68 | ```
|
48 | 69 |
|
49 | 70 | ### **Java**
|
50 | 71 |
|
51 | 72 | ```java
|
52 |
| - |
| 73 | +class Solution { |
| 74 | + public int[] findMissingAndRepeatedValues(int[][] grid) { |
| 75 | + int n = grid.length; |
| 76 | + int[] cnt = new int[n * n + 1]; |
| 77 | + int[] ans = new int[2]; |
| 78 | + for (int[] row : grid) { |
| 79 | + for (int x : row) { |
| 80 | + if (++cnt[x] == 2) { |
| 81 | + ans[0] = x; |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + for (int x = 1;; ++x) { |
| 86 | + if (cnt[x] == 0) { |
| 87 | + ans[1] = x; |
| 88 | + return ans; |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | +} |
53 | 93 | ```
|
54 | 94 |
|
55 | 95 | ### **C++**
|
56 | 96 |
|
57 | 97 | ```cpp
|
58 |
| - |
| 98 | +class Solution { |
| 99 | +public: |
| 100 | + vector<int> findMissingAndRepeatedValues(vector<vector<int>>& grid) { |
| 101 | + int n = grid.size(); |
| 102 | + vector<int> cnt(n * n + 1); |
| 103 | + vector<int> ans(2); |
| 104 | + for (auto& row : grid) { |
| 105 | + for (int x : row) { |
| 106 | + if (++cnt[x] == 2) { |
| 107 | + ans[0] = x; |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + for (int x = 1;; ++x) { |
| 112 | + if (cnt[x] == 0) { |
| 113 | + ans[1] = x; |
| 114 | + return ans; |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | +}; |
59 | 119 | ```
|
60 | 120 |
|
61 | 121 | ### **Go**
|
62 | 122 |
|
63 | 123 | ```go
|
| 124 | +func findMissingAndRepeatedValues(grid [][]int) []int { |
| 125 | + n := len(grid) |
| 126 | + ans := make([]int, 2) |
| 127 | + cnt := make([]int, n*n+1) |
| 128 | + for _, row := range grid { |
| 129 | + for _, x := range row { |
| 130 | + cnt[x]++ |
| 131 | + if cnt[x] == 2 { |
| 132 | + ans[0] = x |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + for x := 1; ; x++ { |
| 137 | + if cnt[x] == 0 { |
| 138 | + ans[1] = x |
| 139 | + return ans |
| 140 | + } |
| 141 | + } |
| 142 | +} |
| 143 | +``` |
64 | 144 |
|
| 145 | +### **TypeScript** |
| 146 | + |
| 147 | +```ts |
| 148 | +function findMissingAndRepeatedValues(grid: number[][]): number[] { |
| 149 | + const n = grid.length; |
| 150 | + const cnt: number[] = Array(n * n + 1).fill(0); |
| 151 | + const ans: number[] = Array(2).fill(0); |
| 152 | + for (const row of grid) { |
| 153 | + for (const x of row) { |
| 154 | + if (++cnt[x] === 2) { |
| 155 | + ans[0] = x; |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + for (let x = 1; ; ++x) { |
| 160 | + if (cnt[x] === 0) { |
| 161 | + ans[1] = x; |
| 162 | + return ans; |
| 163 | + } |
| 164 | + } |
| 165 | +} |
65 | 166 | ```
|
66 | 167 |
|
67 | 168 | ### **...**
|
|
0 commit comments