|
73 | 73 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
74 | 74 |
|
75 | 75 | ```python
|
76 |
| - |
| 76 | +class Solution: |
| 77 | + def digArtifacts(self, n: int, artifacts: List[List[int]], dig: List[List[int]]) -> int: |
| 78 | + def check(artifact): |
| 79 | + r1, c1, r2, c2 = artifact |
| 80 | + for x in range(r1, r2 + 1): |
| 81 | + for y in range(c1, c2 + 1): |
| 82 | + if (x, y) not in s: |
| 83 | + return False |
| 84 | + return True |
| 85 | + |
| 86 | + s = {(i, j) for i, j in dig} |
| 87 | + return sum(check(v) for v in artifacts) |
77 | 88 | ```
|
78 | 89 |
|
79 | 90 | ### **Java**
|
80 | 91 |
|
81 | 92 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
82 | 93 |
|
83 | 94 | ```java
|
| 95 | +class Solution { |
| 96 | + public int digArtifacts(int n, int[][] artifacts, int[][] dig) { |
| 97 | + Set<Integer> s = new HashSet<>(); |
| 98 | + for (int[] d : dig) { |
| 99 | + s.add(d[0] * n + d[1]); |
| 100 | + } |
| 101 | + int ans = 0; |
| 102 | + for (int[] a : artifacts) { |
| 103 | + if (check(a, s, n)) { |
| 104 | + ++ans; |
| 105 | + } |
| 106 | + } |
| 107 | + return ans; |
| 108 | + } |
84 | 109 |
|
| 110 | + private boolean check(int[] a, Set<Integer> s, int n) { |
| 111 | + int r1 = a[0], c1 = a[1], r2 = a[2], c2 = a[3]; |
| 112 | + for (int i = r1; i <= r2; ++i) { |
| 113 | + for (int j = c1; j <= c2; ++j) { |
| 114 | + if (!s.contains(i * n + j)) { |
| 115 | + return false; |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + return true; |
| 120 | + } |
| 121 | +} |
85 | 122 | ```
|
86 | 123 |
|
87 | 124 | ### **TypeScript**
|
@@ -112,6 +149,65 @@ function digArtifacts(
|
112 | 149 | }
|
113 | 150 | ```
|
114 | 151 |
|
| 152 | +### **C++** |
| 153 | + |
| 154 | +```cpp |
| 155 | +class Solution { |
| 156 | +public: |
| 157 | + int digArtifacts(int n, vector<vector<int>>& artifacts, vector<vector<int>>& dig) { |
| 158 | + unordered_set<int> s; |
| 159 | + for (auto& d : dig) s.insert(d[0] * n + d[1]); |
| 160 | + int ans = 0; |
| 161 | + for (auto& a : artifacts) ans += check(a, s, n); |
| 162 | + return ans; |
| 163 | + } |
| 164 | + |
| 165 | + bool check(vector<int>& a, unordered_set<int>& s, int n) { |
| 166 | + int r1 = a[0], c1 = a[1], r2 = a[2], c2 = a[3]; |
| 167 | + for (int i = r1; i <= r2; ++i) |
| 168 | + { |
| 169 | + for (int j = c1; j <= c2; ++j) |
| 170 | + { |
| 171 | + if (!s.count(i * n + j)) |
| 172 | + { |
| 173 | + return false; |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + return true; |
| 178 | + } |
| 179 | +}; |
| 180 | +``` |
| 181 | + |
| 182 | +### **Go** |
| 183 | + |
| 184 | +```go |
| 185 | +func digArtifacts(n int, artifacts [][]int, dig [][]int) int { |
| 186 | + s := map[int]bool{} |
| 187 | + for _, d := range dig { |
| 188 | + s[d[0]*n+d[1]] = true |
| 189 | + } |
| 190 | + check := func(a []int) bool { |
| 191 | + r1, c1, r2, c2 := a[0], a[1], a[2], a[3] |
| 192 | + for i := r1; i <= r2; i++ { |
| 193 | + for j := c1; j <= c2; j++ { |
| 194 | + if !s[i*n+j] { |
| 195 | + return false |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | + return true |
| 200 | + } |
| 201 | + ans := 0 |
| 202 | + for _, a := range artifacts { |
| 203 | + if check(a) { |
| 204 | + ans++ |
| 205 | + } |
| 206 | + } |
| 207 | + return ans |
| 208 | +} |
| 209 | +``` |
| 210 | + |
115 | 211 | ### **...**
|
116 | 212 |
|
117 | 213 | ```
|
|
0 commit comments