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 filled) is not necessarily solvable. Only the filled cells need to be validated.

原题链接:https://oj.leetcode.com/problems/valid-sudoku/

按照数独的规则,一行、一列、对角线、9个小格中不出现同一个数字。

import java.util.ArrayList;
import java.util.List;

public class ValidSudoku {
	public boolean isValidSudoku(char[][] board) {
		for(int i=0;i<9;i++){
			List<Character> list = new ArrayList<Character>();
			for(int j=0;j<9;j++)
				list.add(board[i][j]);
			if(!isValid(list))
				return false;
		}
		for(int i=0;i<9;i++){
			List<Character> list = new ArrayList<Character>();
			for(int j=0;j<9;j++)
				list.add(board[j][i]);
			if(!isValid(list))
				return false;
		}
		for(int i=0;i<3;i++){
			for(int j=0;j<3;j++){
				List<Character> list = new ArrayList<Character>();
				for(int k=0;k<3;k++){
					for(int l=0;l<3;l++){
						list.add(board[i*3+k][j*3+l]);
					}
				}
				if(!isValid(list))
					return false;
			}
		}
		return true;
	}
	private boolean isValid(List<Character> list){
		for(Character ch : list){
			if(ch != '.')
				if(list.indexOf(ch) != list.lastIndexOf(ch))
					return false;
		}
		return true;
	}
}
时间: 2024-10-05 17:20:13

LeetCode——Valid 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]Valid Sudoku

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

LeetCode: Valid Sudoku 解题报告

Valid SudokuDetermine 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

[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)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]Valid Sudoku 解题报告 C 语言

[题目] 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 '.'. Note: A valid Sudoku board (partially filled) is not necessarily solvable.