Leetcode 36.Valid Sudoku (effective Sudoku) thinking and method of solving problems

Source: Internet
Author: User

Valid Sudoku

Determine if a Sudoku is valid, according To:sudoku puzzles-the Rules.

The Sudoku board could 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.


Ideas: The topic is very simple, the main rule of understanding, Sudoku game has not played, do not know what the rules, I think any 9 squares 1-9 of the number is at most 1, who knows the rule is a specific nine lattice 1-9 of the number of the maximum is 1, other not considered. The code is verbose, but the idea is clear, as follows:

public class Solution {//static variable map<character,integer> Map = new hashmap<character,integer> ();            public boolean Isvalidsudoku (char[][] board) {//judgment per line for (int i = 0; i < board.length; i++) { Initmap ();//need to initialize for (int j = 0; J < Board[0].length, J + +) for each time {//is the number if (board[                        I][J] >= ' 0 ' && board[i][j] <= ' 9 ') {if (Map.get (board[i][j]) > 0) {//Description repeat Number                    return false;                    }else{Map.put (board[i][j],1); }}else if (board[i][j]! = '. ')        {//appear spaces and characters other than 0-9 return false;//direct return false}} }//Judge each column for (int i = 0; i < board[0].length; i++) {initmap ();//Initialize for (int j =) each time. 0; J < Board.length;    J + +) {//is the number if (Board[j][i] >= ' 0 ' && board[j][i] <= ' 9 ') {                if (Map.get (Board[j][i]) > 0) {//Description repeat number return false;                    }else{Map.put (board[j][i],1); }}else if (board[j][i]! = '. ')        {//appear spaces and characters other than 0-9 return false;//direct return false}} }//Judge nine for (int i = 0; i < board.length-2; i = i+3) {//Line {for (int j = 0; J < Board[0].le Ngth-2; j=j+3) {initmap ();//Initialize for (int m = i, M < i + 3;m++) {for (int n = j; n < j+3;                            n++) {//is the number if (Board[m][n] >= ' 0 ' && board[m][n] <= ' 9 ') {                            if (Map.get (Board[m][n]) > 0) {//Description repeat number return false;                            }else{Map.put (board[m][n],1); }}else if (board[m][n]! = '. ') {//appearsA space and a character other than 0-9 return false;//directly returns false}}    }}} return true;        }//Initialize map assigns a value of 0 private void Initmap () {for (char i = ' 0 '; I <= ' 9 '; i++) {map.put (i,0) to each key; }    }}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Leetcode 36.Valid Sudoku (effective Sudoku) thinking and method of solving problems

Contact Us

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.