zoukankan      html  css  js  c++  java
  • Valid Sudoku

    1. Title

    36. Valid Sudoku

    2. Http address

    https://leetcode.com/problems/valid-sudoku/?tab=Description

    3. The question

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.

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

    4 My code(AC)

    public class Solution {
        
    
    public boolean isValidSudoku(char[][] board) {
    
            if (board == null || board.length < 1) {
                return true;
            }
    
            int row = board.length;
            int col = board[0].length;
    
            Set<Character> set = null;
    
            for (int i = 0; i < row; i++) {
                set = new HashSet<Character>();
                for (int j = 0; j < col; j++) {
                    char tmp = board[i][j];
                    if ( tmp != '.' && set.contains(tmp)) {
                        return false;
                    } else {
                        set.add(tmp);
                    }
                }
            }
    
            for (int i = 0; i < col; i++) {
                set = new HashSet<Character>();
                for (int j = 0; j < row; j++) {
                    char tmp = board[j][i];
                    if (tmp != '.' && set.contains(tmp)) {
                        return false;
                    }
                    set.add(tmp);
                }
            }
    
            int rowMin = 0;
            int rowMax = 2;
            int colMin = 0;
            int colMax = 2;
    
            while (rowMax < row && colMax < col) {
    
                if (!check(board, colMin, colMax, rowMin, rowMax)) {
                    return false;
                } 
    
                int[] tmp = { rowMin, rowMax, colMin, colMax };
    
                while (colMax < col - 1) {
                    colMin += 3;
                    colMax += 3;
                    if (!check(board, colMin, colMax, rowMin, rowMax)) {
                        return false;
                    }
                }
    
                colMin = tmp[2];
                colMax = tmp[3];
                while (rowMax < row - 1) {
                    rowMin += 3;
                    rowMax += 3;
                    if (!check(board, colMin, colMax, rowMin, rowMax)) {
                        return false;
                    }
                }
    
                rowMin = tmp[0] + 3;
                rowMax = tmp[1] + 3;
                colMin = tmp[2] + 3;
                colMax = tmp[3] + 3;
            }
    
            return true;
    
        }
    
        public boolean check(char board[][], int colMin, int colMax, int rowMin, int rowMax) {
            Set<Character>     set = new HashSet<Character>();
            for (int i = rowMin; i <= rowMax; i++) {
            
                for (int j = colMin; j <= colMax; j++) {
                    char tmp = board[i][j];
                    if (tmp != '.' && set.contains(tmp)) {
                        return false;
                    } else {
                        set.add(tmp);
                    }
                }
            }
    
            return true;
        }
    
    }
  • 相关阅读:
    python
    基于vue的npm发包
    div实现水平垂直居中
    element-ui中表格添加fixed定位列后 出现表格错位现象
    解决github经常无法访问的问题
    快排算法C语言实现
    ubuntu下qt运行时/usr/bin/ld: cannot find -lGL
    llinux装完qt 启动qtcreator报错
    LINUX权限-bash: ./startup.sh: Permission denied
    In short, don’t use a pointer to a string literal if you plan to alter the string.
  • 原文地址:https://www.cnblogs.com/ordili/p/6432280.html
Copyright © 2011-2022 走看看