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

 

 

解答:

package com.jack.algorithm;

import java.util.HashSet;

/**
 * create by jack 2019/5/4
 *
 * @author jack
 * @date: 2019/5/4 22:09
 * @Description:
 */
public class ValidSudoku {
    /**
     * 36-題目描述:
     * https://leetcode.com/problems/valid-sudoku/
     * 判斷輸入的二維字符數組是否符合要求:
     *
     *  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.
     * @param board
     * @return
     */
    public static boolean isValidSudoku(char[][] board) {
        boolean valid = true;
        // check rows
        for(int r = 0; r < 9; r++)
        {
            valid = valid && isValidRow(board, r);
        }// check columns
        for(int c = 0; c < 9; c++)
        {
            valid = valid && isValidColumn(board, c);
        }
        // check boxes
        for(int r = 0; r < 9; r += 3)
        {
            for(int c = 0; c < 9; c += 3)
            {
                valid = valid && isValidBox(board, r, c);
            }
        }
        // if any of the methods returned false, valid will be false
        // else it will be true
        return valid;
    }


    /**
     * 檢查行是否符合要求,每一行是否包含重複出現的數字
     * @param board
     * @param r
     * @return
     */
    public static boolean isValidRow(char[][] board, int r)
    {
        HashSet<Character> set = new HashSet<>();

        for(int a = 0; a < 9; a++)
        {
            char current = board[r][a];
            //如果是點則跳過
            if (current == '.') {
                continue;
            }
            //如果包含返回false
            if(set.contains(current)) {
                return false;
            } else {
                set.add(current);
            }
        }
        return true;
    }


    /**
     * 每一列是否包含相同的數字
     * @param board
     * @param c
     * @return
     */
    public static boolean isValidColumn(char[][] board, int c)
    {
        HashSet<Character> set = new HashSet<>();

        for(int a = 0; a < 9; a++)
        {
            char current = board[a][c];
            //如果是“.”則跳過
            if(current == '.') {
                continue;
            }
            //如果包含返回false
            if(set.contains(current)) {
                return false;
            } else {
                set.add(current);
            }
        }
        return true;
    }

    /**
     * 檢查3*3的矩陣是否符合要求
     * @param board
     * @param r
     * @param c
     * @return
     */
    public static boolean isValidBox(char[][] board, int r, int c)
    {
        HashSet<Character> set = new HashSet<>();
        for(int a = r; a < r + 3; a++)
        {
            for(int b = c; b < c + 3; b++)
            {
                char current = board[a][b];
                //是“.”就跳過
                if(current == '.') {
                    continue;
                }
                //包含返回false
                if(set.contains(current)) {
                    return false;
                } else {
                    set.add(current);
                }
            }
        }
        return true;
    }




    public static char[][] getCharArray(String [][] strs) {
        char[][] chars = new char[9][9];
        for (int i=0;i<9;i++) {
            for (int j=0;j<9;j++) {
                chars[i][j] = strs[i][j].charAt(0);
            }
        }
        return chars;

    }

    public static void displayChar(char[][] chars) {
        for (int i=0;i<9;i++) {
            for (int j=0;j<9;j++) {
                System.out.print("'"+chars[i][j]+"'"+",");
            }
            System.out.println("\n");
        }
    }

    public static void main(String[] args) {
        String[][] strs = new String[][]{
                {"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"}
        };
        char[][] chars = getCharArray(strs);
        //displayChar(chars);
        boolean valid = isValidSudoku(chars);
        System.out.println("valid="+valid);

    }
}

 

 

源碼:

源碼

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章