class Solution { public: int equalPairs(vector<vector>& grid) { int ans = 0, n = grid.size(); vector<vector> grid_roted(n, vector(n, 0)); //转置 for(int j=0; j<n; j++) for(int i=0; i<n; i++) grid_roted[j][i] = grid[i][j];

    for(int i=0; i<n; i++)
        for(int j=0; j<n; j++)
            if(grid[i]==grid_roted[j]) ans += 1;

    return ans;
}

};//转换成Dart内容:class Solution { int equalPairs(List<List> grid) { int ans = 0; int n = grid.length; List<List> gridRoted = List.generate(n, (index) => List.filled(n, 0)); //转置 for (int j = 0; j < n; j++) { for (int i = 0; i < n; i++) { gridRoted[j][i] = grid[i][j]; } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (grid[i] == gridRoted[j]) { ans += 1; } } } return ans; } }

C++ and Dart Solutions for Counting Equal Pairs in a 2D Grid

原文地址: https://www.cveoy.top/t/topic/qhIF 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录