首页 > 其他 > 详细

* Valid Sudoku

时间:2015-12-25 07:40:23      阅读:168      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!