8000 Update 36. Valid Sudoku.js by Tahirc1 · Pull Request #14 · Tahirc1/LeetcodeJs · GitHub
[go: up one dir, main page]

Skip to content

Update 36. Valid Sudoku.js #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions 31-40/36. Valid Sudoku.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
var isValidSudoku = function(board) {
// create variable to store row , col and box values
let row = []
let col = []
let box = []
// loop i over 0-8
for (let i = 0; i <= 8; i++){
// loop j over 0-8
for (let j = 0; j <= 8; j++){
// this will help us get position of each cell from board
let IndI = 3*Math.floor(i/3) + Math.floor(j/3)
let IndJ = 3*(i % 3) + (j % 3)
// check if value is already in row , col or box and return false
if (row.indexOf(board[i][j]) != -1 ){return false}
if (col.indexOf(board[j][i]) != -1 ){return false}
if (box.indexOf(board[IndI][IndJ]) != -1){return false}

// if not then add value to row , col and box
if(board[i][j] != '.'){row.push(board[i][j])}
if(board[j][i] != '.'){col.push(board[j][i])}
if(board[IndI][IndJ] != '.'){box.push(board[IndI][IndJ])}
}
// reset row , col and box array
row = []
col = []
box = []
}

return true
}
}
0