Java LeetCode 每日一题

        LeetCodeExercise

package JavaLeetCodeExercise20240831;

import java.util.Collections;

public class LeetCodeExercise {
    public static void main(String[] args) {
        char[][] grid = {{'B','W','B'},{'B','W','W'},{'B','W','B'}};
        Solution solution = new Solution();
        System.out.println(solution.canMakeSquare(grid));
    }
}
class Solution {
    public boolean canMakeSquare(char[][] grid) {
        for (int i = 0; i <= 1; i++) {
            for (int j = 0; j <= 1; j++) {
                if (check(grid, i, j)) {
                    return true;
                }
            }
        }
        return false;
    }

    public boolean check(char[][] grid, int row, int col) {
        int count = 0;
        // 这个变量用于统计白色块在3*3矩阵中的4个2*2子矩阵的个数,如果等于2,则不可以;若不是2,则可以
        // 因为当2*2的矩阵中,同一个颜色已经出现两次了,就不可能通过只改变一个块的颜色构造颜色相同的正方形
        // 假如一个颜色出现0次,那么说明已经构成了另外一个颜色的相同正方形
        // 假如一个颜色出现1次,那么说明可以通过改变该颜色,构成另外一个颜色的相同正方形
        // 假如一个颜色出现4次(最多4次),那么已经构成颜色相同的正方形
        for (int i = row; i <= row + 1; i++) {
            for (int j = col; j <= col + 1; j++) {
                if (grid[i][j] == 'W') {
                    count++;
                }
            }
        }
        if (count == 2) {
            return false;
        }
        return true;
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值