原题描述Description:
Sudoku Background
Sudoku is a game played on a 9x9 grid. The goal of the game is to fill all cells of the grid with digits from 1 to 9, so that each column, each row, and each of the nine 3x3 sub-grids (also known as blocks) contain all of the digits from 1 to 9.
(More info at: http://en.wikipedia.org/wiki/Sudoku)
Sudoku Solution Validator
Write a function validSolution that accepts a 2D array representing a Sudoku board, and returns true if it is a valid solution, or false otherwise. The cells of the sudoku board may also contain 0's, which will represent empty cells. Boards containing one or more zeroes are considered to be invalid solutions.
题意就是让同学们做一个数独验证器,能够检测输入的一个简单二位数组(board)是否是一个已经完成(没有0,0表示未完成的空格)且正确(符合数独规则,即横竖、9个小宫格里面都有且只有1-9)
这道题目从大方向上来说应该用验证性的方法,即有错就return False,排错到最后return True
下面是几种思路:
1.活用sorted()函数可以免去排序的麻烦
correct = [1, 2, 3, 4

本博客介绍如何编写一个Python函数validSolution,用于验证数独解决方案的正确性。函数接受一个9x9的二维数组,其中0表示空格,并检查行、列和3x3宫格是否符合数独规则。提供了三种不同的实现方法,包括使用sorted()函数、通过列表推导式检查重复元素以及使用set()进行去重。
最低0.47元/天 解锁文章

被折叠的 条评论
为什么被折叠?



