10.LeetCode第九题--有效的数独--哈希表的经典应用

这篇博客探讨了两种验证9x9数独有效性的算法。第一种方法利用哈希映射来跟踪行、列和子数独中的数字出现次数,第二种方法则采用布尔型数组实现,效率更高。这两种方法都在单次迭代中完成检查,确保行、列和3x3子数独内没有重复数字。
摘要由CSDN通过智能技术生成

思路

一个简单的解决方案是遍历该 9 x 9 数独 三 次,以确保:

行中没有重复的数字。
列中没有重复的数字。
3 x 3 子数独内没有重复的数字。
实际上,所有这一切都可以在一次迭代中完成。

方法:一次迭代
首先,让我们来讨论下面两个问题:

如何枚举子数独?
可以使用 box_index = (row / 3) * 3 + columns / 3,其中 / 是整数除法。

注意:此处是我做本题时候没有想到的一个点

如何确保行 / 列 / 子数独中没有重复项?
可以利用 value -> count 哈希映射来跟踪所有已经遇到的值。

现在,我们完成了这个算法的所有准备工作:

遍历数独。
检查看到每个单元格值是否已经在当前的行 / 列 / 子数独中出现过:
如果出现重复,返回 false。
如果没有,则保留此值以进行进一步跟踪。
返回 true。

class Solution {
    public boolean isValidSudoku(char[][] board) {
        HashMap<Integer,Integer> row[] = new HashMap[9];
        HashMap<Integer,Integer> col[] = new HashMap[9];
        HashMap<Integer,Integer> box[] = new HashMap[9];
        for(int i=0;i<9;i++)
        {
            row[i] = new HashMap<Integer,Integer>();
            col[i] = new HashMap<Integer,Integer>();
            box[i] = new HashMap<Integer,Integer>();
        }
        for(int i=0;i<9;i++)
        {
            for(int j=0;j<9;j++)
            {
                char num = board[i][j];
                if(num != '.')
                {
                    int n = (int)num;
                    int box_index = (i/3)*3 + (j/3);
                    row[i].put(n,row[i].getOrDefault(n,0)+1);
                    col[j].put(n,col[j].getOrDefault(n,0)+1);
                    box[box_index].put(n,box[box_index].getOrDefault(n,0)+1);

                    if(row[i].get(n)>1||col[j].get(n)>1||box[box_index].get(n)>1)
                    {
                        return false;
                    } 
               }
            }
        }
        return true;
    }
}

思路二

上面一种解法采用哈希表较为耗时,采用数组的方法效率更高

class Solution {
    public boolean isValidSudoku(char[][] board) {
        boolean[][] row = new boolean[9][9];
        boolean[][] col = new boolean[9][9];
        boolean[][] box = new boolean[9][9];

        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                if(board[i][j] != '.') {
                    int num = board[i][j] - '1';
                    int boxIndex = (i / 3) * 3 + j / 3;
                    if(row[i][num]  || col[j][num] || box[boxIndex][num]) {
                        return false;
                    }

                    row[i][num] = true;
                    col[j][num] = true;
                    box[boxIndex][num] = true;
                }
            }
        }
        return true;
    }
}
class Solution {
    //官方用的hashmap,但采用数组更方便
    public boolean isValidSudoku(char[][] board) {
        int[][] rows = new int[9][9];
        int[][] col = new int[9][9];
        int[][] sbox = new int[9][9];
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                if (board[i][j]!='.'){
                    int num = board[i][j] - '1';
                    int index_box = (i/3)*3+j/3;
                    if (rows[i][num]==1) { return false;}
                    else { rows[i][num]=1; }
                    if (col[j][num]==1) { return false;}
                    else { col[j][num]=1; }
                    if (sbox[index_box][num]==1)  { return false;}
                    else { sbox[index_box][num]=1; }
                }
            }
        }
        return true;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值