zoukankan      html  css  js  c++  java
  • 36. Valid Sudoku

    题目:

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

    A partially filled sudoku which is valid.

    Note:
    A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.

    链接: http://leetcode.com/problems/valid-sudoku/

    题解:

    验证数独,分三次验证行,列以及9个3x3子块就可以了。

    Time Complexity - O(n2), Space Complexity - O(1)。

    public class Solution {
        public boolean isValidSudoku(char[][] board) {
            if(board == null || board.length == 0)
                return false;
            HashSet<Character> set = new HashSet<Character>();
            
            for(int i = 0; i < 9; i++) {
                for(int j = 0; j < 9; j++) {
                    if(board[i][j] != '.' && set.contains(board[i][j]))
                        return false;
                    set.add(board[i][j]);
                }
                set.clear();
            }
            
            for(int j = 0; j < 9; j++) {
                for(int i = 0; i < 9; i++) {
                    if(board[i][j] != '.' && set.contains(board[i][j]))
                        return false;
                    set.add(board[i][j]);
                }
                set.clear();
            }
            
            for(int i = 1; i < 9; i += 3) {
                for(int j = 1; j < 9; j += 3) {
                    for(int k = -1; k <= 1; k++) {
                        for(int l = -1; l <= 1; l++) {
                            if(board[i + k][j + l] != '.' && set.contains(board[i + k][j + l]))
                                return false;
                            set.add(board[i + k][j + l]);
                        }
                    }
                    set.clear();
                }
            }
             
            return true;   
        }
    }

    二刷:

    Java:

    依然是 Time Complexity - O(n2) , Space Complexity - O(1)。这样遍历了三次数组,并不是很快,可以继续优化,只遍历一次数组。

    public class Solution {
        public boolean isValidSudoku(char[][] board) {
            if (board == null || board.length == 0) {
                return false;
            }
            Set<Character> set = new HashSet<>();
            for (int i = 0; i < board.length; i++) {
                for (int j = 0; j < board[0].length; j++) {
                    if (board[i][j] != '.' && !set.add(board[i][j])) {
                        return false;
                    }
                }
                set.clear();
            }
            
            for (int j = 0; j < board[0].length; j++) {
                for (int i = 0; i < board.length; i++) {
                    if (board[i][j] != '.' && !set.add(board[i][j])) {
                        return false;
                    }
                }
                set.clear();
            }       
            
            for (int i = 1; i < board.length; i += 3) {
                for (int j = 1; j < board[0].length; j += 3) {
                    for (int k = -1; k <= 1; k++) {
                        for (int l = -1; l <= 1; l++) {
                            if (board[i + k][j + l] != '.' && !set.add(board[i + k][j + l])) {
                                return false;
                            }
                        }
                    }
                    set.clear();
                }
            }
            return true;
        }
    }

    只遍历一次数组,代码主要来自于Lorraine921:

    public class Solution {
        public boolean isValidSudoku(char[][] board) {
            if (board == null || board.length == 0) {
                return false;
            }
            Set<Character> rows = new HashSet<Character>();
            Set<Character> cols = new HashSet<Character>();
            Set<Character> cube = new HashSet<Character>();
            for(int i = 0; i < 9; i++){
                for (int j = 0; j < 9;j++){
                    if(board[i][j] != '.' && !rows.add(board[i][j])) {
                        return false;
                    }
                    if(board[j][i] != '.' && !cols.add(board[j][i])) {
                        return false;
                    }
                    int RowIndex = 3 * (i / 3);
                    int ColIndex = 3 * (i % 3);
                    if(board[RowIndex + j / 3][ColIndex + j % 3]!= '.' && !cube.add(board[RowIndex + j / 3][ColIndex + j % 3])) {
                        return false;
                    }
                }
                rows.clear();
                cols.clear();
                cube.clear();
            }
            return true;
        }
    }

    三刷:

    看来三刷并没有吸取到二刷的精华,上面的解法完全忘记了。需要再领会。

    Java:

    public class Solution {
        public boolean isValidSudoku(char[][] board) {  // assume len = 9
            if (board == null || board.length == 0) {
                return false;
            }
            Set<Integer> set = new HashSet<>();
            for (int i = 0; i < 9; i++) {
                set.clear();
                for (int j = 0; j < 9; j++) {
                    if (board[i][j] != '.' && !set.add(board[i][j] - '0')) {
                        return false;
                    }
                }
            }
            for (int j = 0; j < 9; j++) {
                set.clear();
                for (int i = 0; i < 9; i++) {
                    if (board[i][j] != '.' && !set.add(board[i][j] - '0')) {
                        return false;
                    }
                }
            }
            for (int i = 1; i < 9; i += 3) {
                for (int j = 1; j < 9; j += 3) {
                    set.clear();
                    for (int k = -1; k <= 1; k++) {
                        for (int l = -1; l <= 1; l++) {
                            if (board[i + k][j + l] != '.' && !set.add(board[i + k][j + l] - '0')) {
                                return false;
                            }
                        }
                    }
                }
            }
            return true;
        }
    }

    Reference:

    https://leetcode.com/discuss/48737/1-7-lines-python-4-solutions

    https://leetcode.com/discuss/27272/shared-my-concise-java-code

    https://leetcode.com/discuss/23901/my-short-solution-by-c-o-n2

    https://leetcode.com/discuss/17990/sharing-my-easy-understand-java-solution-using-set

    https://leetcode.com/discuss/92445/yet-another-java-2ms-solution

  • 相关阅读:
    JavaScript 浮点数处理
    从输入URL到浏览器显示页面发生了什么
    hadoop 编译任意版本的eclipse 插件
    Stm32F103面向对象编程之GPIO
    flume 1.7 安装与使用
    HIVE分组排序问题
    MapReduce实现共同朋友问题
    hadoop项目实战--ETL--(三)实现mysql表到HIVE表的全量导入与增量导入
    hadoop项目实战--ETL--(二)实现自动向mysql中添加数据
    hadoop项目实战--ETL--(一)项目分析
  • 原文地址:https://www.cnblogs.com/yrbbest/p/4436318.html
Copyright © 2011-2022 走看看