Leetcode#36Valid Sudoku

Valid Sudoku

Total Accepted: 33785 Total Submissions: 123877My Submissions

Question Solution

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.

分析:每行中每个元素可以为空,但0~9数字只出现只能出现一次;

每列中每个元素可以为空,但0~9数字只出现只能出现一次;

每个小九宫格中每个元素可以为空,但0~9数字只出现只能出现一次;

public class Solution {

boolean isV(int xs, int xe, int ys, int ye, char[][] board)

{

Map<Character,Integer> z=new HashMap<Character,Integer>();

for(int i=xs;i<=xe;i++)

for(int j=ys;j<=ye;j++)

if(board[i][j]>=‘0‘&&board[i][j]<=‘9‘)

if(z.get(board[i][j])!=null)

return false;

else

z.put(board[i][j],1);

return true;

}

public boolean isValidSudoku(char[][] board) {

Map<Character,Integer> x;

Map<Character,Integer> y;

for(int i=0;i<9;i++)

{

x=new HashMap<Character,Integer>();

y=new HashMap<Character,Integer>();

for(int j=0;j<9;j++)

{

if(board[i][j]>=‘0‘&&board[i][j]<=‘9‘)

if(x.get(board[i][j])!=null)

return false;

else

x.put(board[i][j],1);

if(board[j][i]>=‘0‘&&board[j][i]<=‘9‘)

if(y.get(board[j][i])!=null)

return false;

else

y.put(board[j][i],1);

}

x.clear();

y.clear();

}

if(isV(0,2,0,2,board)&&isV(3,5,0,2,board)&&isV(6,8,0,2,board)&&isV(0,2,3,5,board)&&isV(3,5,3,5,board)&&isV(6,8,3,5,board)&&isV(0,2,6,8,board)&&isV(3,5,6,8,board)&&isV(6,8,6,8,board))

return true;

else

return false;

}

}

时间: 2024-10-16 04:12:23

Leetcode#36Valid Sudoku的相关文章

LeetCode: Valid Sudoku [035]

[题目] 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 (part

LeetCode:Valid Sudoku,Sudoku Solver(数独游戏)

Valid Sudoku 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 boa

[leetcode]Valid Sudoku @ Python

原题地址:https://oj.leetcode.com/problems/valid-sudoku/ 题意: 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 sudo

Leetcode | Valid Sudoku &amp; Sudoku Solver

判断valid,没有更好的方法,只能brute force. 1 class Solution { 2 public: 3 bool isValidSudoku(vector<vector<char> > &board) { 4 5 int n; 6 for (int i = 0; i < 9; ++i) { 7 vector<bool> contained(9, false); 8 for (int j = 0; j < 9; ++j) { 9 i

【leetcode】 Sudoku Solver

问题: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution. A sudoku puzzle... ...and its solution numbers marked in red. 说明: 数独有

[LeetCode][JavaScript]Sudoku Solver

Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution. A sudoku puzzle... ...and its solution numbers marked in re

LeetCode——Valid Sudoku

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

[LeetCode] 37. Sudoku Solver 求解数独

Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each row. Each of the digits 1-9 must occur exactly once in each column.

[LeetCode]Valid Sudoku

检测数独是否合格. 思路: 填充一遍就知道是否合格. 基本暴力搜索的思想. 1 /*************************************************************************************************** 2 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. 3 The Sudoku board could be parti