[Leetcode 36&37] Valid Sudoku & Sudoku Solver (Sudoku problem)

Source: Internet
Author: User

`Import java.util.arrays;/** * Determine if a Sudoku is valid, according To:sudoku puzzles-the rules.the Sudoku Board Co Uld be partially filled, where empty cells is 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 Validsudoku {//number of solutions//every digit from 1 to 9 must appear:1-9 these nine numbers must appear only once in various cases//in each of the Colum NS, each column//in each of the rows,//and per row in every of the nine boxes. Nine Test//501/501 cases e:369 ms//submitted:1 minute ago//Check if the Sudoku is valid//Time complexity O (n^2) space complexity O (n) public boolean Isvalidsudoku (char[][] board) {BOOL  ean[] flag = new boolean[10];//mark//Check nine lines for (int i = 0; i < 9; i++) {Arrays.fill (flag, false); for (int j = 0; j < 9; J + +) {if (board[i][j] = = '. ') Continue;if (!flag[board[i][j]-' 0 ']) {flag[board[i][j]-' 0 '] = true;} else {return false;}}} Check nine columns for (int j = 0; J <9; J + +) {Arrays.fill (flag, false), for (int i = 0; i < 9; i++) {if (board[i][j] = = '. ')    Continue;if (!flag[board[i][j]-' 0 ']) {flag[board[i][j]-' 0 '] = true;} else {return false;}}} Check nine nine for (int i = 0; i < 9; i + = 3) {for (int j = 0; J < 9; J + = 3) {Arrays.fill (flag, false); for (int i1 = 0; I 1 < 3; i1++) {for (int j1 = 0; J1 < 3; j1++) {if (board[i + i1][j + j1] = = '. ')    Continue;if (!flag[board[i + i1][j + J1]-' 0 ']) {flag[board[i + i1][j + J1]-' 0 '] = true;} else {return false;}}}}    return true; }public static void Main (string[] args) {//TODO auto-generated method stub}}`

`/** * Write A program to solve a Sudoku puzzle by filling the empty cells. Empty cells is indicated by the character '. Assume that there would be is only one unique solution. * */public class SudokuSolver {//6/6 test Cases passed.//status:accepted//runtime:260 ms//submitted:0 minutes ago//input    Sudoku only unique solution//backtracking//Time complexity O (n^4) space complexity O (1) public void Solvesudoku (char[][] board) {SOLVESUDOKU1 (board); public boolean solveSudoku1 (char[][] board) {for (int i = 0, i < 9; i++) {for (int j = 0; J < 9; j+ +) {if (board[i][j] = = '. ') {for (int k = 1; k <= 9; k++) {Board[i][j] = (char) (' 0 ' + K); if (IsValid (board, I, J) && SOLVESUDOKU1 (board)) { return true;} else {board[i][j] = '. ';    /backtracking method}}return false;}}}    return true; } public boolean isValid (char[][] board, int i, int j) {//Check the owning row for (int k = 0; k < 9; k++) {if (i! = k && Amp    BOARD[I][J] = = Board[k][j]) return false;} Check the owning column for (int k = 0; k < 9; k++) {if (j! = k && BOARD[I][J] = = Board[i][k]) return false;} Check the owning nine lattice for (int i1 = 3 * (I/3), I1 < (I/3 + 1) * 3; I1 + +) {for (int j1 = 3 * (J/3); J1 < (J/3 + 1) * 3; J 1 + +) {if (I1! = && J1! = J && board[i][j] = = Board[i1][j1]) return false;}    return true; } public static void Main (string[] args) {//TODO auto-generated method stub}}`

[Leetcode 36&37] Valid Sudoku & Sudoku Solver (Sudoku problem)

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• Sales Support

1 on 1 presale consultation

• After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.