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

* Valid Sudoku

时间:2015-12-25 07:43:04      阅读:173      评论: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 public class Solution {
 2 public boolean isValidSudoku(char[][] board) {
 3     for (int i=0; i<9; i++) {
 4         if (!isParticallyValid(board,i,0,i,8)) return false;
 5         if (!isParticallyValid(board,0,i,8,i)) return false;
 6     }
 7     for (int i=0;i<3;i++){
 8         for(int j=0;j<3;j++){
 9             if (!isParticallyValid(board,i*3,j*3,i*3+2,j*3+2)) return false;
10         }
11     }
12     return true;
13 }
14 private boolean isParticallyValid(char[][] board, int x1, int y1,int x2,int y2){
15     Set singleSet = new HashSet();
16     for (int i= x1; i<=x2; i++){
17         for (int j=y1;j<=y2; j++){
18             if (board[i][j]!=‘.‘) if(!singleSet.add(board[i][j])) return false; // If this set already contains the element, the call leaves the set unchanged and returns false.
19         }
20     }
21     return true;
22 }
23 
24 }

https://leetcode.com/discuss/17990/sharing-my-easy-understand-java-solution-using-set

* Valid Sudoku

标签:

原文地址:http://www.cnblogs.com/hygeia/p/5074821.html

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