Uber VO 面试真题解析:Sudoku Validity Check 数组遍历与集合判重

24次阅读
没有评论

Sudoku is a number-placement puzzle. The objective is to fill a 9 x 9 grid with digits so that each column, each row, and each of the nine 3 x 3 sub-grids that compose the grid contains all of the digits from 1 to 9.

This algorithm should check if the given grid of numbers represents a correct solution to Sudoku.

Example

For the first example below, the output should be true. For the other grid, the output should be false: each of the nine 3 x 3 sub-grids should contain all of the digits from 1 to 9.

  • [execution time limit] 3 seconds (java)
  • [memory limit] 1 GB
  • [input] array.array.integer grid

A matrix representing 9 x 9 grid already filled with numbers from 1 to 9.

  • [output] boolean

true if the given grid represents a correct solution to Sudoku, false otherwise.

这道题要求判断一个已经填满的 9×9 数独盘面是否合法。核心做法是分别检查 9 行、9 列和 9 个 3×3 宫格,确保每一组都恰好包含 1 到 9 且不重复。实现时通常可以用布尔数组、Set 或按位标记来做去重,遍历一次矩阵即可完成验证,时间复杂度很适合在线评测。

正文完
 0