魔法师 (@Constanline) 在 Leetcode每日一题 —— 3546. 等和矩阵分割 I 中发帖
思路
先遍历一遍求出总和与一半,然后横向/纵向各遍历一遍检查是否能组成一半的值。
代码
class Solution {
public boolean canPartitionGrid(int[][] grid) {
int m = grid.length;
int n = grid[0].length;
// 先遍历一遍求出总和
long sum = 0;
for (int[] row : grid) {
for (int num : row) {
sum += num;
}
}
// 如果是奇数直接返回false
if ((sum & 1) == 1) {
...