码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode Valid Sudoku

时间:2015-02-04 00:19:40      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:

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.

对于这道题来说,只是判断已存在的数字是否满足数独的规则,因此直接判断即可

1.查看每一列是否有重复的数字

2.查看每一行是否有重复的数字

3.查看每一个方格是否有重复的数字

 1 package Valid.Sudoku;
 2 
 3 public class ValidSudoku {
 4     //每一行,每一列的看,每一个格子看
 5 public boolean isValidSudoku(char[][] board) {
 6         for(int i=0;i<3;i++){
 7             for(int j=0;j<3;j++){
 8                 boolean indicator=this.isValidCell(board, i*3, j*3);
 9                 if(!indicator){
10                     return indicator;
11                 }
12             }
13         }
14         for(int i=0;i<9;i++){
15             boolean indicator=this.isValidRow(board, i);
16             if(!indicator){
17                 return indicator;
18             }
19         }
20         for(int i=0;i<9;i++){
21             boolean indicator=this.isValidCol(board, i);
22             if(!indicator){
23                 return indicator;
24             }
25         }
26         return true;
27     }
28 private boolean isValidCell(char[][] board,int row,int col){
29     boolean [] vectory=new boolean[10];
30     //初始化
31     for(int i=0;i<9;i++){
32         vectory[i]=false;
33     }
34     for(int i=0;i<3;i++){
35         for(int j=0;j<3;j++){
36             int temp=board[i+row][j+col]-‘0‘;
37             if(temp > 0 && temp <= 9 && !vectory[temp]){
38                 vectory[temp]=true;
39             }else if(temp >= 0 && temp <= 9 && vectory[temp]){
40                 return false;
41             }
42         }
43     }
44     return true;
45 }
46 private boolean isValidRow(char[][]board,int row){
47     boolean [] vectory=new boolean[10];
48     //初始化
49     for(int i=0;i<9;i++){
50         vectory[i]=false;
51     }
52     for(int i=0;i<9;i++){
53         int temp=board[row][i]-‘0‘;
54         if(temp > 0 && temp <= 9 && !vectory[temp]){
55             vectory[temp]=true;
56         }else if(temp > 0 && temp <= 9 && vectory[temp]){
57             return false;
58         }
59     }
60     return true;
61 }
62 private boolean isValidCol(char[][]board,int col){
63     boolean [] vectory=new boolean[10];
64     //初始化
65     for(int i=0;i<9;i++){
66         vectory[i]=false;
67     }
68     for(int i=0;i<9;i++){
69         int temp=board[i][col]-‘0‘;
70         if(temp >0 && temp <= 9 && !vectory[temp]){
71             vectory[temp]=true;
72         }else if(temp > 0 && temp <= 9 && vectory[temp]){
73             return false;
74         }
75     }
76     return true;
77 }
78 }

 

Leetcode Valid Sudoku

标签:

原文地址:http://www.cnblogs.com/criseRabbit/p/4271271.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!