【LeetCode】36. Valid Sudoku

亡梦爱人 提交于 2019-11-30 22:07:01

Difficulty: Medium

 More:【目录】LeetCode Java实现

Description

https://leetcode.com/problems/valid-sudoku/

Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:

  1. Each row must contain the digits 1-9 without repetition.
  2. Each column must contain the digits 1-9 without repetition.
  3. Each of the 9 3x3 sub-boxes of the grid must contain the digits 1-9 without repetition.


A partially filled sudoku which is valid.

The Sudoku board could be partially filled, where empty cells are filled with the character '.'.

Example 1:

Input:  [    ["5","3",".",".","7",".",".",".","."],    ["6",".",".","1","9","5",".",".","."],    [".","9","8",".",".",".",".","6","."],    ["8",".",".",".","6",".",".",".","3"],    ["4",".",".","8",".","3",".",".","1"],    ["7",".",".",".","2",".",".",".","6"],    [".","6",".",".",".",".","2","8","."],    [".",".",".","4","1","9",".",".","5"],    [".",".",".",".","8",".",".","7","9"]  ]  Output: true  

Example 2:

Input:  [    ["8","3",".",".","7",".",".",".","."],    ["6",".",".","1","9","5",".",".","."],    [".","9","8",".",".",".",".","6","."],    ["8",".",".",".","6",".",".",".","3"],    ["4",".",".","8",".","3",".",".","1"],    ["7",".",".",".","2",".",".",".","6"],    [".","6",".",".",".",".","2","8","."],    [".",".",".","4","1","9",".",".","5"],    [".",".",".",".","8",".",".","7","9"]  ]  Output: false  Explanation: Same as Example 1, except with the 5 in the top left corner being       modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid.  

Note:

  • A Sudoku board (partially filled) could be valid but is not necessarily solvable.
  • Only the filled cells need to be validated according to the mentioned rules.
  • The given board contain only digits 1-9 and the character '.'.
  • The given board size is always 9x9.

Intuition

Mehtod 1: Use three hashSet (rowSet, colSet, boxSet) to store  digits in each row/ column/ sub-box.

Details: 

  i: 1 ->9   j: i ->9

  rowSet: a[i][j]

  colSet: a[j][i]

  boxSet: 

    For a given sub-box, we can traverse a 3*3 sub-box using just j: a[ j/3 ] [ j%3 ]

    We has 9 sub-boxes, so we can use i to switch in different sub-boxes: a[i/3*3+j/3][i%3*3+j%3]

Mehtod 2: Use only one hashSet to store a[i][j] in three ways.

  Encode a[i][j] as a[i][j]+"r"+i  to represent a[i][j] in row i;

  Encode a[i][j] as a[i][j]+"c"+j  to represent a[i][j] in colum j; 

  Encode a[i][j] as a[i][j]+"box"+i/3+j/3  to represent a[i][j] in box-i/3-j/3; 

 

Solution

    //Method 1      public boolean isValidSudoku(char[][] board) {          for(int i=0; i<9; i++){              HashSet<Character> rowSet = new HashSet<>();              HashSet<Character> colSet = new HashSet<>();              HashSet<Character> boxSet = new HashSet<>();              for(int j=0; j<9; j++){                  if(board[i][j]!='.' && !rowSet.add(board[i][j]))                      return false;                  if(board[j][i]!='.' && !colSet.add(board[j][i]))                      return false;                  if(board[i/3*3+j/3][i%3*3+j%3]!='.' && !boxSet.add(board[i/3*3+j/3][i%3*3+j%3]))                      return false;              }          }          return true;      }            //Method 2: easier to understand      public boolean isValidSudoku(char[][] board) {          HashSet<String> set = new HashSet<>();          for(int i=0; i<9; i++){              for(int j=0; j<9; j++){                  if(board[i][j]!='.'){                      String num = String.valueOf(board[i][j]);                      if(!set.add(num+"r"+i) || !set.add(num+"c"+j) || !set.add(num+"box"+i/3+j/3))                          return false;                                      }              }          }          return true;      }  

  

Complexity

Time complexity : O(n)

Space complexity : O(n)

 

What I've learned

1. Learn how to use i, j to represent elements in different rows and columns and sub-boxes.

2. For a given sub-box, we can traverse a 3*3 sub-box using just ja[ j/3 ] [ j%3 ]

3. It's clever to store elements in a hashSet by convert elements to String.

 

 More:【目录】LeetCode Java实现

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!