LeetCode(38)-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 ‘.‘.

![这里写图片描述](http://img.blog.csdn.net/20160409183641502)
A partially filled sudoku which is valid.

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

思路:

  • 首先这是一道数独题目,关于数独的性质,可以参考下面的链接

    数独规则

  • 考虑根据数独的各个条件来逐个解决,需要判断每行,每一列,以及每个小方格是否是1~9,出现一次,思路是建立一个list添加,判断是否出现重复,出现返回false;同样如果board == null,或者行数和列数不等于9,也返回false
  • -

代码:

public class Solution {
    public boolean isValidSudoku(char[][] board) {
        ArrayList<ArrayList<Character>> rows = new ArrayList<ArrayList<Character>>();
        ArrayList<ArrayList<Character>> cols = new ArrayList<ArrayList<Character>>();
        ArrayList<ArrayList<Character>> boxs = new ArrayList<ArrayList<Character>>();
        if(board == null || board.length != 9|| board[0].length != 9){
            return false;
        }
        for(int i = 0;i < 9;i++){
            rows.add(new ArrayList<Character>());
            cols.add(new ArrayList<Character>());
            boxs.add(new ArrayList<Character>());
        }
        for(int a = 0;a < board.length;a++){
            for(int b = 0;b < board[0].length;b++){
                if(board[a][b] == ‘.‘){
                    continue;
                }
                ArrayList<Character> row = rows.get(a);
                if(row.contains(board[a][b])){
                    return false;
                }else{
                    row.add(board[a][b]);
                }
                ArrayList<Character> col = cols.get(b);
                if(col.contains(board[a][b])){
                    return false;
                }else{
                    col.add(board[a][b]);
                }
                ArrayList<Character> box = boxs.get(getNum(a,b));
                if(box.contains(board[a][b])){
                    return false;
                }else{
                    box.add(board[a][b]);
                }
            }
        }return true;
    }
    public int getNum(int i,int j){
        return (i/3)*3+j/3;
    }
}
时间: 2024-11-25 13:29:33

LeetCode(38)-Valid Sudoku的相关文章

[LeetCode] 036. Valid Sudoku (Easy) (C++)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 036. Valid Sudoku (Easy) 链接: 题目:https://leetcode.com/problems/valid-sudoku/ 代码(github):https://github.com/illuz/leetcode 题意: 判断一个数独是否有效. 有效的数独不强求有解. 分析: 只要同一行

[LeetCode][JavaScript]Valid Sudoku

https://leetcode.com/problems/valid-sudoku/ 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 sud

【LeetCode】Valid Sudoku

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 boar

leetCode 36.Valid Sudoku(有效的数独) 解题思路和方法

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 boa

LeetCode 36 Valid Sudoku (C,C++,Java,Python)

Problem: 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 (

LeetCode 036 Valid Sudoku

题目要求: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

leetCode 36. Valid Sudoku(数独) 哈希

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 S

Java for LeetCode 036 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 '.'. 解题思路: 传说中的数独(九宫格)问题,老实遍历三个规则即可: JAVA实现: static public boolean isValidSudoku(cha

LeetCode 36 Valid Sudoku(合法的数独)

题目链接: https://leetcode.com/problems/valid-sudoku/?tab=Description 给出一个二维数组,数组大小为数独的大小,即9*9 其中,未填入数字的数组值为’.’ 判断当前所给已知数组中所填的数字是否合法. 数独合法性判断: 1. 满足每一行的数字都只能是1~9,并且不能产生重复 2. 满足每一列的数字都只能是1~9,并且不能产生重复 3. 满足每一个3*3的正方形块中的数字只能是1~9,并且不能产生重复 判断过程: 初始化三个数组:row,