[leetcode]Valid Sudoku
来源:互联网 发布:冯卡门知乎 编辑:程序博客网 时间:2024/05/16 04:47
问题描述:
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.
代码:
public class Valid_Sudoku { //javapublic boolean isValidSudoku(char[][] board) { int size = 9;int [] member = new int[size]; //record if is occur; //valid rowfor(int i = 0; i < 9; i++){for(int k = 0; k < 9; k++)member[k] = 0;for(int j = 0; j < 9; j++){if(board[i][j] == '.')continue;int pos = board[i][j]-'0';if(member[pos-1] == 1)return false;else member[pos-1] = 1;}}//valid colfor(int i = 0; i < 9; i++){for(int k = 0; k < 9; k++)member[k] = 0;for(int j = 0; j < 9; j++){if(board[j][i] == '.')continue;int pos = board[j][i]-'0';if(member[pos-1] == 1)return false;else member[pos-1] = 1;}}//valid cubefor(int ibegin = 0; ibegin < 9; ibegin = ibegin+3){for(int jbegin = 0; jbegin < 9; jbegin = jbegin+3){for(int k = 0; k < 9; k++)member[k] = 0;for(int i = ibegin; i < ibegin+3; i++){for(int j = jbegin; j < jbegin+3; j++){if(board[i][j] == '.')continue;int pos = board[i][j]-'0';if(member[pos-1] == 1)return false;else member[pos-1] = 1;}}}}return true; }public static void main(String [] args){String[] boardStr = {"......5..", ".........", ".........", "93..2.4..", "..7...3..", ".........", "...34....", ".....3...", ".....52.."};char [][] board = new char [9][9];for(int i =0; i< boardStr.length; i++){for(int j = 0; j<boardStr[i].length(); j++){board[i][j] = boardStr[i].charAt(j);}}Valid_Sudoku vs = new Valid_Sudoku();System.out.println(vs.isValidSudoku(board));}}
0 0
- 【LeetCode】Valid Sudoku && Sudoku Solver
- [Leetcode]Sudoku Solver&&Valid Sudoku
- leetcode Valid Sudoku & Sudoku Solver
- LeetCode : Valid Sudoku
- [LeetCode]Valid Sudoku
- [leetcode] Valid Sudoku
- LeetCode-Valid Sudoku
- LeetCode Valid Sudoku
- LeetCode - Valid Sudoku
- LeetCode:Valid Sudoku
- leetcode-036:Valid Sudoku
- Leetcode: Valid Sudoku
- LeetCode题解:Valid Sudoku
- 【LeetCode】 Valid Sudoku
- Leetcode Valid Sudoku
- LeetCode | Valid Sudoku
- LeetCode Valid Sudoku
- [Leetcode] Valid Sudoku (Java)
- JAVA面向对象——继承
- Codeforces Round #275 (Div. 2) —— C
- Leetcode: Convert Sorted List to Binary Search Tree
- 关于LibSVM的SMO问题
- UIWebView之获取所点位置图片URL
- [leetcode]Valid Sudoku
- JAVA应用闪退,堆栈日志中出现EXCEPTION_STACK_OVERFLOW
- chromium启动过程-线程篇
- Mac OS打开idea时提示需要Java SE 6 runtime的解决办法
- postgresql-9.3安装
- LINUX的字符设备驱动基本原理用到单片机系统中
- [科普文]ubuntu上安装Apache2+ModSecurity及自定义WAF规则
- js生成Excel
- matlab函数详解之“padarray”