Re: [閒聊] 每日LeetCode已回收
https://leetcode.com/problems/equal-row-and-column-pairs/description/
2352. Equal Row and Column Pairs
給定一個矩陣grid,判斷有幾個完全相等的一對行列,例如:
[1,2,3] 和 [1 兩個相等。
,2
,3]
Example 1:
https://assets.leetcode.com/uploads/2022/06/01/ex1.jpg

Input: grid = [[3,2,1],[1,7,6],[2,7,7]]
Output: 1
Explanation: There is 1 equal row and column pair:
- (Row 2, Column 1): [2,7,7]
Example 2:
https://assets.leetcode.com/uploads/2022/06/01/ex2.jpg

Input: grid = [[3,1,2,2],[1,4,4,5],[2,4,2,2],[2,4,2,2]]
Output: 3
Explanation: There are 3 equal row and column pairs:
- (Row 0, Column 0): [3,1,2,2]
- (Row 2, Column 2): [2,4,2,2]
- (Row 3, Column 2): [2,4,2,2]
思路:
1.把每一行和每一列的每個元素直接比較是否相等並計數。
Java Code:
-------------------------------------------------
class Solution {
public int equalPairs(int[][] grid) {
int n = grid.length;
int count = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (isEqual(grid, i, j)) {
count++;
}
}
}
return count;
}
private boolean isEqual(int[][] grid, int row, int col) {
int n = grid.length;
for (int i = 0; i < n; i++) {
if (grid[row][i] != grid[i][col]) {
return false;
}
}
return true;
}
}
-------------------------------------------------
--
https://i.imgur.com/fHpKflu.jpg

--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 122.100.75.86 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1686641031.A.E07.html
→
06/13 15:25,
2年前
, 1F
06/13 15:25, 1F
推
06/13 15:25,
2年前
, 2F
06/13 15:25, 2F
→
06/13 15:44,
2年前
, 3F
06/13 15:44, 3F
推
06/13 16:15,
2年前
, 4F
06/13 16:15, 4F
討論串 (同標題文章)
完整討論串 (本文為第 346 之 719 篇):