Posts Tagged 【math】Valid Sudoku
来源:互联网 发布:重庆时时彩源码 编辑:程序博客网 时间:2024/06/12 07:48
Valid Sudoku
Total Accepted: 33528 Total Submissions: 122751My SubmissionsDetermine 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.
/*这道题目就是让你看看当前状态的是否符合Sudoku就是每一行,每一列,每一个方块的看而不是确认是否有解*/public class Solution { public boolean isValidSudoku(char[][] board) { int[] row = new int[9]; int[] column = new int[9]; boolean flag = false; //row && cloumn for(int i = 0;i<9;i++) { for(int k = 0;k<9;k++) { row[k] = 0; column[k] = 0; } for(int j = 0;j<9;j++) { if(board[i][j] != '.') { if(row[board[i][j]-'1']>0) return false; else { row[board[i][j]-'1']++; } } if(board[j][i] != '.') { if(column[board[j][i]-'1']>0) return false; else { column[board[j][i]-'1']++; } } } } //block for(int i = 0;i<9;i = i+3) for(int j = 0;j<9;j = j + 3) { for(int k = 0;k<9;k++) row[k] = 0; for(int m = 0;m<3;m++) for(int n = 0;n<3;n++) { if(board[i+m][j+n] != '.') { if(row[board[i+m][j+n]-'1']>0) return false; else { row[board[i+m][j+n]-'1']++; } } } } // return true; }}
/*将block放在一个行列循环中block[i][j]表示第i个(从左至右从上至下)方块中的元素j*/public class Solution { public boolean isValidSudoku(char[][] board) { int[] row = new int[9]; int[] column = new int[9]; int[][] block = new int[9][9]; boolean flag = false; for(int i = 0;i<9;i++) { for(int k = 0;k<9;k++) { row[k] = 0; column[k] = 0; } for(int j = 0;j<9;j++) { //cloumn if(board[i][j] != '.') { if(row[board[i][j]-'1']>0) return false; else { row[board[i][j]-'1']++; } } //cloumn if(board[j][i] != '.') { if(column[board[j][i]-'1']>0) return false; else { column[board[j][i]-'1']++; } } //block[i][j]表示第i个(从左至右从上至下)方块中的元素j if(board[i][j] != '.') { if(block[i/3*3+j/3][board[i][j]-'1']>0) return false; else { block[i/3*3+j/3][board[i][j]-'1']++; } } } } // return true; }}
Have you met this question in a real interview?
Yes
0 0
- Posts Tagged 【math】Valid Sudoku
- Posts Tagged 【math】Sudoku Solver
- Posts Tagged 【Math】
- Posts Tagged 【math】Count Primes
- Posts Tagged 【math】Happy Number
- Posts Tagged 【fsm】 Valid Number
- Posts Tagged 【dp】Longest Valid Parentheses
- Posts Tagged 【math】Max Points on a Line
- Posts Tagged ‘多站’
- Posts Tagged 【dfs】 Combinations
- Posts Tagged 【tree && dfs】
- Posts Tagged 【String】
- Posts Tagged 【Array】
- Posts Tagged 【Search】
- Posts Tagged 【Catalan】
- Posts Tagged 【List】Anagrams
- POSTS TAGGED: A/B TEST
- Posts Tagged 【dfs】Generate Parentheses
- C++的强制类型转换
- oracle10g/11g 新特性三(Flashback Table)
- 数据库各个派系的起源和应用场景
- leetcode--Remove Linked List Elements
- 第三章第十八题sum
- Posts Tagged 【math】Valid Sudoku
- Fiddler中session的请求/响应类型与图标对照表
- 把本地分支的文件上传至GitLab
- SQL Sever连接不上——SQL Sever服务远程过程调用失败
- C语言中声明、定义和初始化的区别
- 余弦方法计算相似度算法实现
- [what if]第1期——一颗0.9倍光速的棒球
- 亚像素级角点检测(cornerSubPix)
- ANT task之Junit、JunitReport