其他分享
首页 > 其他分享> > 2021.09.19 - 057.有效的数独

2021.09.19 - 057.有效的数独

作者:互联网

文章目录

1. 题目

在这里插入图片描述
在这里插入图片描述

2. 思路

(1) Map

(2) 暴力循环

3. 代码

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class Test {
    public static void main(String[] args) {
    }
}

class Solution {
    public boolean isValidSudoku(char[][] board) {
        Map<Character, Set<Integer>> map = new HashMap<>();
        Set<Integer> set;
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                if (board[i][j] != '.') {
                    if (map.containsKey(board[i][j])) {
                        if (map.get(board[i][j]).contains(i * 10 + j)) {
                            return false;
                        } else {
                            for (int k = 0; k < 9; k++) {
                                map.get(board[i][j]).add(i * 10 + k);
                            }
                            for (int k = 0; k < 9; k++) {
                                map.get(board[i][j]).add(k * 10 + j);
                            }
                            for (int k = 0; k < 3; k++) {
                                for (int l = 0; l < 3; l++) {
                                    map.get(board[i][j]).add(i / 3 * 30 + j / 3 * 3 + k * 10 + l);
                                }
                            }
                        }
                    } else {
                        set = new HashSet<>();
                        for (int k = 0; k < 9; k++) {
                            set.add(i * 10 + k);
                        }
                        for (int k = 0; k < 9; k++) {
                            set.add(k * 10 + j);
                        }
                        for (int k = 0; k < 3; k++) {
                            for (int l = 0; l < 3; l++) {
                                set.add(i / 3 * 30 + j / 3 * 3 + k * 10 + l);
                            }
                        }
                        map.put(board[i][j], set);
                    }
                }
            }
        }
        return true;
    }
}

class Solution1 {
    public boolean isValidSudoku(char[][] board) {
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                if (board[i][j] != '.') {
                    for (int k = 0; k < 9; k++) {
                        if (k != j && board[i][k] == board[i][j]) {
                            return false;
                        }
                    }
                    for (int k = 0; k < 9; k++) {
                        if (k != i && board[k][j] == board[i][j]) {
                            return false;
                        }
                    }
                    for (int k = i / 3 * 3; k < i / 3 * 3 + 3; k++) {
                        for (int l = j / 3 * 3; l < j / 3 * 3 + 3; l++) {
                            if (k != i && l != j && board[k][l] == board[i][j]) {
                                return false;
                            }
                        }
                    }
                }
            }
        }
        return true;
    }
}

标签:10,map,19,2021.09,add,++,int,board,057
来源: https://blog.csdn.net/qq_44021223/article/details/120379736