leetCode 36.Valid Sudoku(有效的数独) 解题思路和方法

Valid Sudoku

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.

The Sudoku board could be partially filled, where empty cells are filled with the character ‘.‘.

A partially filled sudoku which is valid.

Note:

A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.

思路:题目很简单,主要是规则的理解,数独的游戏没有玩过,不知道什么规则,我以为任意9个方格1-9的个数都至多为1,谁知规则是特定的九个格内1-9的个数至多为1,其他不考虑。代码比较啰嗦,但思路清晰,如下:

public class Solution {
    //置为静态变量
    static Map<Character,Integer> map = new HashMap<Character,Integer>();
    public boolean isValidSudoku(char[][] board) {
        //判断每行
        for(int i = 0; i < board.length; i++){
            initMap();//每次均需初始化
            for(int j = 0; j < board[0].length; j++){
                //是数字
                if(board[i][j] >= '0' && board[i][j] <= '9'){
                    if(map.get(board[i][j]) > 0){//说明重复数字
                        return false;
                    }else{
                        map.put(board[i][j],1);
                    }
                }else if(board[i][j] != '.'){//出现空格和0-9之外的字符
                    return false;//直接返回false
                }
            }
        }
        //判断每列
        for(int i = 0; i < board[0].length; i++){
            initMap();//每次均需初始化
            for(int j = 0; j < board.length; j++){
                //是数字
                if(board[j][i] >= '0' && board[j][i] <= '9'){
                    if(map.get(board[j][i]) > 0){//说明重复数字
                        return false;
                    }else{
                        map.put(board[j][i],1);
                    }
                }else if(board[j][i] != '.'){//出现空格和0-9之外的字符
                    return false;//直接返回false
                }
            }
        }
        //判断九宫格
        for(int i = 0; i < board.length - 2; i = i+3){//行{
            for(int j = 0; j < board[0].length - 2; j=j+3){
                initMap();//初始化
                for(int m = i; m < i + 3;m++){
                    for(int n = j; n < j+3; n++){
                        //是数字
                        if(board[m][n] >= '0' && board[m][n] <= '9'){
                            if(map.get(board[m][n]) > 0){//说明重复数字
                                return false;
                            }else{
                                map.put(board[m][n],1);
                            }
                        }else if(board[m][n] != '.'){//出现空格和0-9之外的字符
                            return false;//直接返回false
                        }
                    }
                }
            }
        }
        return true;
    }
    //初始化map为每个key均赋值0
    private void initMap(){
        for(char i = '0';i <= '9'; i++){
            map.put(i,0);
        }
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-07 00:13:46

leetCode 36.Valid Sudoku(有效的数独) 解题思路和方法的相关文章

LeetCode 36 Valid Sudoku(合法的数独)

题目链接: https://leetcode.com/problems/valid-sudoku/?tab=Description 给出一个二维数组,数组大小为数独的大小,即9*9 其中,未填入数字的数组值为’.’ 判断当前所给已知数组中所填的数字是否合法. 数独合法性判断: 1. 满足每一行的数字都只能是1~9,并且不能产生重复 2. 满足每一列的数字都只能是1~9,并且不能产生重复 3. 满足每一个3*3的正方形块中的数字只能是1~9,并且不能产生重复 判断过程: 初始化三个数组:row,

leetCode 20.Valid Parentheses (有效的括号) 解题思路和方法

Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" a

leetCode 36. Valid Sudoku(数独) 哈希

36. Valid Sudoku(合法数独) Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. Note:A valid S

LeetCode 36 Valid Sudoku (C,C++,Java,Python)

Problem: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. Note: A valid Sudoku board (

36. Valid Sudoku 有效的数独

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. Note: A valid Sudoku board (partially

leetCode 47.Permutations II (排列组合II) 解题思路和方法

Permutations II Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], and [2,1,1]. 思路:这题相比于上一题,是去除了反复项. 代码上与上题略有区别.详细代码例如以下

leetCode 35.Search Insert Position (搜索插入位置) 解题思路和方法

Search Insert Position Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here are few examples. [1,3,5

leetCode 64.Minimum Path Sum (最短路) 解题思路和方法

Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. 思路:此题和前面几个机器人的题非常相像,只是变化了一点,具体代码和

leetCode 45.Jump Game II (跳跃游戏) 解题思路和方法

Jump Game II Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum nu