leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0840.cpp (969B)
0 class Solution { 1 public: 2 int numMagicSquaresInside(const vector<vector<int>> &grid) const { 3 const int n = size(grid), m = size(grid[0]); 4 int res = 0; 5 6 for (int i = 1; i < n - 1; i++) { 7 for (int j = 1; j < m - 1; j++) { 8 if (grid[i][j] != 5) continue; 9 10 if (grid[i - 1][j - 1] + grid[i - 1][j] + grid[i - 1][j + 1] != 15) continue; 11 if (grid[i - 1][j - 1] + grid[i][j - 1] + grid[i + 1][j - 1] != 15) continue; 12 13 unordered_set<int> us; 14 for (int k = i - 1; k <= i + 1; k++) { 15 for (int l = j - 1; l <= j + 1; l++) { 16 if (grid[k][l] < 1 || grid[k][l] > 9) goto next; 17 if (us.count(grid[k][l])) goto next; 18 us.insert(grid[k][l]); 19 } 20 } 21 22 res++; 23 next:; 24 } 25 } 26 27 return res; 28 } 29 };