From 101bf452e3d053ef50fe5c7d60808707bcc16b8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=9E=97=E5=B3=B0?= Date: Thu, 8 Nov 2018 14:39:39 +0800 Subject: [PATCH] 36. Valid Sudoku (java) --- solution/0036.Valid Sudoku/Solution.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 solution/0036.Valid Sudoku/Solution.java diff --git a/solution/0036.Valid Sudoku/Solution.java b/solution/0036.Valid Sudoku/Solution.java new file mode 100644 index 0000000000000..3878f7a77a15d --- /dev/null +++ b/solution/0036.Valid Sudoku/Solution.java @@ -0,0 +1,14 @@ +class Solution { + public boolean isValidSudoku(char[][] board) { + for(int i = 0; i < 9; i++) { + HashSet col = new HashSet<>() , row = new HashSet<>() , cube = new HashSet<>(); + for(int j = 0; j < 9; j++) { + if(board[i][j] != '.' && !row.add(board[i][j])) return false; + if(board[j][i] != '.' && !col.add(board[j][i])) return false; + int colIndex = i/3*3+j/3 , rowIndex = i%3*3+j%3; + if(board[rowIndex][colIndex] != '.' && !cube.add(board[rowIndex][colIndex])) return false; + } + } + return true; + } +} \ No newline at end of file