Nuggets team number online, help you Offer rimmon! Click to see details
Valid Sudoku (Question number 36)
The title
Determine if a 9×9 sudoku is valid. Just verify that the entered numbers are valid according to the following rules.
The numbers 1-9 May appear only once in a row. The numbers 1-9 May appear only once in each column. The numbers 1-9 occur only once in each 3×3 uterus separated by thick solid lines.
The Spaces in sudoku sections have been filled in with numbers, and the Spaces are represented by ‘.’.
Example 1:
Input: [[" 5 ", "3", ""," ", "7", ""," ", ""," "], [" 6 ", ""," ", "1", "9", "5", ""," ", ""], [".", "9", "eight" and ". ", ""," ", ""," 6 ", ""]. [" 8 "and". ", ""," ", "6", ""," ", ""," 3 "], [" 4 ", ""," ", "eight" and ". ", "3", ""," ", "1"], [" 7 ", ""," ", ""," 2 ", ""," ", ""," 6 "]. [". ", "6", ""," ", ""," ", "2", "eight", ""], [".", ""," ", "4", "1", "9", ""," ", "5"]. [".",".",".",".",".",".","8","."," 7","9"]] Output: trueCopy the code
Example 2:
Input: [[" 8 ", "3", ""," ", "7", ""," ", ""," "], [" 6 ", ""," ", "1", "9", "5", ""," ", ""], [".", "9", "eight" and ". ", ""," ", ""," 6 ", ""]. [" 8 "and". ", ""," ", "6", ""," ", ""," 3 "], [" 4 ", ""," ", "eight" and ". ", "3", ""," ", "1"], [" 7 ", ""," ", ""," 2 ", ""," ", ""," 6 "]. [". ", "6", ""," ", ""," ", "2", "eight", ""], [".", ""," ", "4", "1", "9", ""," ", "5"]. [". ", ". ", ""," ", "eight" and ". ", ""," 7 ", "9"]] output: false interpretation: in addition to the first line of the first number changed from 5 to 8, the Spaces within the other Numbers are the same as the sample 1. But because the 3x3 uterus in the upper left corner has two 8s, this sudoku is invalid.Copy the code
Description:
- A valid sudoku (partially filled) is not necessarily solvable.
- You just need to verify that the entered numbers are valid according to the above rules.
- A sudoku sequence is given to contain only numbers
1-9
And character'. '
。 - Given that sudoku is always
9x9
In the form of.
link
Leetcode-cn.com/problems/va…
explain
This problem actually looks like a solution at first glance, but the problem is how to do it.
The main thing is to find the array of row and column blocks and determine whether there is duplicate data inside
Your own answer (array de-duplicate)
function removeDot(arr) { return arr.filter(item => item ! == '.') } function isTurely(arr) { return arr.length === Array.from(new Set(arr)).length } var isValidSudoku = function(board) { for (let i = 0; i < 9; i++) { var rowArr = board[i] if (! isTurely(removeDot(rowArr))) return false var cloArr = board.map(item => item[i]) if (! isTurely(removeDot(cloArr))) return false if ([1, 2, 3].indexOf((i + 1) / 3) >= 0) { var startI = ((i + 1) / 3 - 1) * 3 endI = startI + 3 for (let j = 0; j < 3; j++) { var startJ = j * 3 endJ = startJ + 3 arr = [] while (startJ < endJ) { arr = arr.concat(board[startJ].slice(startI, endI)) startJ++ } if (! isTurely(removeDot(arr))) return false } } } return true };Copy the code
The idea is simple: find the column block, insert data into it, insert data into the array, and then deduplicate the array. If the result of the deduplicate is not the same as the length of the array before the deduplicate, then GG, obviously there is duplication. Here the problem is in the judgment condition, the example to use array to judge is to eat performance of a behavior.
Your own answer (Map, Set)
var isValidSudoku = function(board) { var newMap = new Map() for (let i = 0; i < 9; i++) { for (let j = 0; j < 9; j++) { var boardValue = board[i][j] if (boardValue ! == '.') { var mapNameArr = [`line-${i}`, `row-${j}`, `${parseInt(i / 3)}-${parseInt(j / 3)}`] for (let k = 0; k < mapNameArr.length; k++) { var mapName = mapNameArr[k] var mapRes = newMap.get(mapName) if (mapRes) { if (mapRes.has(boardValue)) return false mapRes.add(boardValue) newMap.set(mapName, mapRes) } else { newMap.set(mapName, new Set([boardValue])) } } } } } return true };Copy the code
This is actually I read some answers after their own and write the answer, there is a reference suspect.
So if you compare it to the first solution, it’s easy to see that you’re actually getting rid of two extra functions here, because the criteria are written inside.
Next, loop 1 through 9 twice directly. It looks simpler than the loop above, but the point is to use the properties of two-dimensional arrays. Then get the column and column block values inside the loop, put them in the Set for comparison, if they exist, GG, do not exist, add them, indeed a lot of convenience, and Map and Set have performance advantages, but because of the number of variables generated, the space complexity needs to be improved.
Better method (separate check)
/** * @param {character[][]} board * @return {boolean} */ var isValidSudoku = function(board) { const mapRow = new Map(), mapColum = new Map() for (let i = 0; i < 9; Maprow.clear () mapcolum.clear () for (let j = 0; j < 9; j++) { if (mapRow.has(board[i][j])) return false if (mapColum.has(board[j][i])) return false if (board[i][j] ! == '.') { mapRow.set(board[i][j], j) } if (board[j][i] ! == '.') { mapColum.set(board[j][i], i) } } } const map = new Map() let m = 0, N = 0 while (m < 9) {map.clear() for (let I = m; i < m + 3; i++) { for (j = n; j < n + 3; j++) { if (map.has(board[i][j])) return false if (board[i][j] ! == '.') { map.set(board[i][j], j) } } } n += 3 } m += 3 n = 0 } return true };Copy the code
In terms of time complexity and space complexity, this is actually a better solution, both of which are on the top.
Here we use the two-layer for loop to check the row and column first, and then use the while to check the block after the completion of the verification, which is also a two-layer for loop, because the loop number is less, the performance is better, each time only loop the current 9 data, a great solution.
In addition, it does not adopt the method of using Set in Map like I did, and the amount of data at the same time is not particularly large, so reading and writing should be faster, which is worth learning
PS: To view past articles and titles, click on the link below:
Here’s 👇 by date
Front Brush path – Directory (Date classification)
After some friends remind, the feeling should also be classified according to the type of questions here is in accordance with the type of 👇
Front brush problem path – Catalogue (Question type classification)
Those who are interested can also check out my personal homepage 👇
Here is RZ