Valid only a few Sudoku

tags: java  Sudoku

Determine if a Sudoku is valid. The Sudoku board could be partially filled, where empty cells are filled with the character '.'.
[img]https://upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Sudoku-by-L2G-20050714.svg/250px-Sudoku-by-L2G-20050714.svg.png[/img]
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.

Detecting whether a valid Sudoku, Sudoku rules we do not understand can go to Baidu. Here we only need to check the current status is valid, instead of finding a solution. So long as we check every row, every column, and if not are not repeated within nine small box numbers appear on it. code show as below:

public class Solution {
public boolean isValidSudoku(char[][] board) {
HashSet<Character> set = new HashSet<Character>();
for(int i = 0; i < board.length; i++) {
for(int j = 0; j < board[0].length; j++) {
if(set.contains(board[i][j]))
return false;
else if(board[i][j] != '.')
set.add(board[i][j]);
}
set.clear();
}

for(int i = 0; i < board[0].length; i++) {
for(int j = 0; j < board.length; j++) {
if(set.contains(board[j][i]))
return false;
else if(board[j][i] != '.')
set.add(board[j][i]);
}
set.clear();
}

for(int m = 0; m < 3; m++) {
for(int n = 0; n < 3; n++) {
for(int i = 3 * m; i < 3 * m + 3; i++) {
for(int j = 3 * n; j < 3 * n + 3; j++) {
if(set.contains(board[i][j]))
return false;
else if(board[i][j] != '.')
set.add(board[i][j]);
}
}
set.clear();
}
}
return true;
}
}

Intelligent Recommendation

leetcode36-Valid Sudoku Sudoku effective

Determining a number of unique 9x9 is valid. only need toAccording to the following rulesVerify whether the figures have been filled can be effective. Digital1-9Only appear once in each row. Digital1-...

leetcode-36- valid Sudoku (valid sudoku) -java

Title and test cases Solution 1 (success, 30ms, medium speed) velocity o (n), the space o (n) in particular a row, a column, a 1-9 find whether squares approach is repeated is added a hashmap, 1-9 as ...

[LeetCode]: 36. Valid Sudoku (Valid Sudoku)

36. Valid Sudoku (Valid Sudoku) Problem Description: Determine whether a 9x9 Sudoku is valid. You only need to verify whether the number you have entered is valid according to the following rules. The...

LeetCode 36. Valid Sudoku Valid Sudoku

Determine whether a 9x9 Sudoku is valid. only need toAccording to the following rulesTo verify that the number you have entered is valid. Numbers1-9It can only appear once per line. Numbers1-9It can o...

leetcode 36 valid sudoku / valid sudoku

Title description: The question is relatively simple, it doesn't let you judge whether the current Sudoku has a solution, only judge whether the current filled number meets the regulations. Problem-so...

More Recommendation

Brushing questions-valid sudoku (valid sudoku)

Title description According to: Sudoku Puzzles-rules to determine whether a Sudoku is valid. The Sudoku board can be partially filled, and empty cells are filled with the characters ‘.’. S...

LeetCode 36. Valid Sudoku Valid Sudoku (Java)

topic: Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules: Each row must contain the digits 1-9 without repetition. Each column must ...

LeetCode: 36. Valid Sudoku (valid Sudoku)

topic Original title link algorithm The requirement of this question is to check whether the nine-square grid given by the question is valid.Only need to detect the number givenThat's it The algorithm...

leetcode [array] ----- 36. Valid Sudoku (valid Sudoku)

1. Title description 2. Analysis Judge whether a 9x9 Sudoku is valid. only need toAccording to the following rules, Just verify that the number already filled in is valid. Number1-9It can only appear ...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top