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, column, cube 均为0

两层嵌套的循环,当遇到board数组中的不是’.’时,将board中的char转换为相对应的整数并且减去1

然后对每一行,每一列以及每一个3*3的正方形进行合法性判断.

参考代码:

package leetcode_50;

import java.util.Arrays;

/***
 *
 * @author pengfei_zheng
 * 判断数组是否合法
 */
public class Solution36 {
    public static boolean isValidSudoku(char[][] board) {
        int [][]row = new int[9][9];
        int [][]column = new int[9][9];
        int [][]cube = new int[9][9];
        for(int i = 0 ; i < 9 ; i ++){
            Arrays.fill(row[i],0);
            Arrays.fill(column[i], 0);
            Arrays.fill(cube[i], 0);
        }
        for(int i = 0 ; i < 9 ; i++){
            for(int j = 0; j < 9 ; j++){
                if(board[i][j]!=‘.‘){
                    //tips: char转为对应的数字需要减去48 此处得到对一个数字并且减去1
                    int num = board[i][j]-49;
                    int k = i / 3 * 3 + j / 3;
                    //这里的num表示当前的在该行下是否已经填入过,如果该num已经使用过则返回
                    if(row[i][num]!=0 || column[j][num]!=0 || cube[k][num]!=0)
                        return false;
                    else
                        row[i][num] = column[j][num] = cube[k][num] = 1;
                }
            }
        }
        //只有当全部的判断均正常进行时,才能返回true
        return true;
    }
}
时间: 2024-12-23 10:45:42

LeetCode 36 Valid Sudoku(合法的数独)的相关文章

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(有效的数独) 解题思路和方法

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 boa

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 (

[LeetCode] 036. Valid Sudoku (Easy) (C++)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 036. Valid Sudoku (Easy) 链接: 题目:https://leetcode.com/problems/valid-sudoku/ 代码(github):https://github.com/illuz/leetcode 题意: 判断一个数独是否有效. 有效的数独不强求有解. 分析: 只要同一行

[Leetcode][Python]36: Valid Sudoku

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 36: Valid Sudokuhttps://oj.leetcode.com/problems/valid-sudoku/ Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty

(LeetCode)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] 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】36 - Valid Sudoku

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.(http://sudoku.com.au/TheRules.aspx) The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. N

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