讨论/《哈希表》 - 有效的数独/
《哈希表》 - 有效的数独
共 5 个回复

Java,HashSet

class Solution {
    public boolean isValidSudoku(char[][] board) {
        Set<Character> x = new HashSet<>();
        for(int i=0;i<9;i++){
            x.clear();
            for(int j=0;j<9;j++){
                if(check(board[i][j])){
                    if(x.contains(board[i][j]))return false;
                    x.add(board[i][j]);
                }
            }
            x.clear();
            for(int j=0;j<9;j++){
                if(check(board[j][i])){
                    if(x.contains(board[j][i]))return false;
                    x.add(board[j][i]);
                }
            }
        }
        for(int i=0;i<9;i+=3){
            for(int j=0;j<9;j+=3){
                x.clear();
                for(int k=0;k<3;k++){
                    for(int o=0;o<3;o++){
                        if(check(board[i+k][j+o])){
                            if(x.contains(board[i+k][j+o]))return false;
                            x.add(board[i+k][j+o]);
                        }
                    }
                }
            }
        }
        return true;
    }
    public boolean check(char a){
        if(a>='1'&&a<='9')return true;
        return false;
    } 

}
1

JavaScript版本 暴力法,效率不高,也没想到更好的办法啦 😅~

执行用时:108 ms, 在所有 JavaScript 提交中击败了63.85%的用户
内存消耗:41.3 MB, 在所有 JavaScript 提交中击败了62.72%的用户

var isValidSudoku = function (board) {
    let map = {}
    //遍历判断每一行是否符合
    for (let i = 0; i < 9; i++) {
        for (let j = 0; j < 9; j++) {
            let num = board[i][j]
            if (num === '.') continue
            if (!map[num]) map[num] = true
            else return false
        }
        map = {}    //遍历完一次后清空,重复利用
    }
    //遍历判断每一列是否符合
    for (let i = 0; i < 9; i++) {
        for (let j = 0; j < 9; j++) {
            let num = board[j][i]
            if (num === '.') continue
            if (!map[num]) map[num] = true
            else return false
        }
        map = {}    //遍历完一次后清空,重复利用
    }
    //定义好需要检查的区块范围,做开又闭
    const position = [
        { xStart: 0, xEnd: 3, yStart: 0, yEnd: 3 },
        { xStart: 3, xEnd: 6, yStart: 0, yEnd: 3 },
        { xStart: 6, xEnd: 9, yStart: 0, yEnd: 3 },
        { xStart: 0, xEnd: 3, yStart: 3, yEnd: 6 },
        { xStart: 3, xEnd: 6, yStart: 3, yEnd: 6 },
        { xStart: 6, xEnd: 9, yStart: 3, yEnd: 6 },
        { xStart: 0, xEnd: 3, yStart: 6, yEnd: 9 },
        { xStart: 3, xEnd: 6, yStart: 6, yEnd: 9 },
        { xStart: 6, xEnd: 9, yStart: 6, yEnd: 9 },
    ]
    for (let p = 0; p < position.length; p++) {
        let boundary = position[p]
        for (let i = boundary.xStart; i < boundary.xEnd; i++) {
            for (let j = boundary.yStart; j < boundary.yEnd; j++) {
                let num = board[i][j]
                if (num === '.') continue
                if (!map[num]) map[num] = true
                else return false
            }

        }
        map = {}    //遍历完一次后清空,重复利用
    }
    return true
};
1
class Solution {
public:
    bool isValidSudoku(vector<vector<char>>& board) {
        std::unordered_set<char> row,column,block;

        for (char i = 0; i < 9; ++i) {
            row.clear();
            column.clear();
            block.clear();
            for (char j = 0; j < 9; ++j) {
                /* check every row */
                if (row.count(board[i][j]) > 0)
                    return false;
                else if ('.' != board[i][j])
                    row.insert(board[i][j]);
                /* check every column */
                if (column.count(board[j][i]) > 0)
                    return false;
                else if ('.' != board[j][i])
                    column.insert(board[j][i]);

                /* check every block */
                if (block.count(board[i/3*3+j/3][i%3*3+j%3]) > 0)
                    return false;
                else if ('.' != board[i/3*3+j/3][i%3*3+j%3])
                    block.insert(board[i/3*3+j/3][i%3*3+j%3]);
            }
        }
        return true;
    }
};

3x3那个一开始没想到好的办法,就硬写了,官方的题解里面有不错的方法,可以参考一下
官方题解

四个for循环。。。。。