leetcode 36. Valid Sudoku || 37. Sudoku Solver

本次题解包括

  • 36. Valid Sudoku
  • 37. Sudoku Solver

36. 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.

题目地址:leetcode Valid Sudoku

题目大意:给你一个数独,让你判断是否是合法的数独。合法的数度定义为:每一行和每一列的数字以及每9个宫格数字不能重复出现。

思路:

先判断行和列是否有重复的,再判断9个是否有重复的。

直观的写法:

C++

 

更好的写法:

C++

python

 

 


 

37. 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 Sudoku Solver

题目大意: 给个数独,让你输出解

思路:

C++

Python

 

本文是leetcode如下的题解

  • 36. Valid Sudoku
  • 37. Sudoku Solver

更多题解可以查看: https://www.hrwhisper.me/leetcode-algorithm-solution/

本博客若无特殊说明则由 hrwhisper 原创发布
转载请点名出处:细语呢喃 > leetcode 36. Valid Sudoku || 37. Sudoku Solver
本文地址:https://www.hrwhisper.me/leetcode-valid-sudoku-solver/

您的支持将鼓励我继续创作!

Leetcode , . permalink.

2 thoughts on “leetcode 36. Valid Sudoku || 37. Sudoku Solver

  1. LN 10: if (board[x / 3 * 3 + i][y / 3 * 3 + j] == board[x][y] && x != x / 3 * 3 + i &&y != y / 3 * 3 + j)

    这里是不是应该改成
    x != x / 3 * 3 + i || y != y / 3 * 3 + j

    否则就会变成对角线啦~ 请多多指教~

    • if (board[x / 3 * 3 + i][y / 3 * 3 + j] == board[x][y] && x != x / 3 * 3 + i &&y != y / 3 * 3 + j) 这个的意思是说x和y的和待判定的点不一样,而不是对角线的意思。
      是为了防止这种情况的发生:board[x / 3 * 3 + i][y / 3 * 3 + j] == board[x / 3 * 3 + i][y / 3 * 3 + j]

Leave a Reply

Your email address will not be published. Required fields are marked *