Valid sudoku, 是否是有效的数独

问题描述:给定9x9矩阵,看是是否是有效数独,不用全部都填上数字,可以为.

算法分析:这道题就是判断,不难,有效数独三个充分条件,行,列,3*3子矩阵,都要满足数字不能重复。

 1 public boolean isValidSudoku(char[][] board)
 2     {
 3         if(board == null || board.length != 9 || board[0].length != 9)
 4         {
 5             return false;
 6         }
 7
 8         //判断行
 9         for(int i = 0; i < 9; i ++)
10         {
11             boolean[] m = new boolean[9];
12             for(int j = 0; j < 9; j ++)
13             {
14                 if(board[i][j] != ‘.‘)
15                 {
16                     //if(m[(int)board[i][j]])这样写是错误的,因为(int)‘1‘不等于1.
17                     if(m[(int)(board[i][j]-‘1‘)])
18                     {
19                         return false;
20                     }
21                     m[(int)(board[i][j]-‘1‘)] = true;
22                 }
23             }
24         }
25
26         //判断列
27         for(int i = 0; i < 9; i ++)
28         {
29             boolean[] m = new boolean[9];
30             for(int j = 0; j < 9; j ++)
31             {
32                 if(board[j][i] != ‘.‘)
33                 {
34                     if(m[(int)(board[j][i]-‘1‘)])
35                     {
36                         return false;
37                     }
38                     m[(int)(board[j][i]-‘1‘)] = true;
39                 }
40             }
41         }
42
43         //判断3*3矩阵,总共有9个
44         for(int k = 0; k < 9; k ++)
45         {
46             boolean[] m = new boolean[9];
47             for(int i = k/3*3; i < k/3*3 + 3; i ++)
48             {
49                 for(int j = k%3*3; j < k%3*3 + 3; j ++)
50                 {
51                     if(board[i][j] != ‘.‘)
52                     {
53                         if(m[(int)(board[i][j]-‘1‘)])
54                         {
55                             return false;
56                         }
57                         m[(int)(board[i][j]-‘1‘)] = true;
58                     }
59                 }
60             }
61         }
62
63         return true;
64     }
时间: 2024-10-13 00:17:02

Valid sudoku, 是否是有效的数独的相关文章

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:Valid Sudoku,Sudoku Solver(数独游戏)

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] 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&amp;37] Valid Sudoku &amp; Sudoku Solver (数独问题)

题目链接:valid-sudoku import java.util.Arrays; /** * 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 whic

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

[LettCode]49. 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

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]Valid Sudoku

检测数独是否合格. 思路: 填充一遍就知道是否合格. 基本暴力搜索的思想. 1 /*************************************************************************************************** 2 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. 3 The Sudoku board could be parti