【LeetCode-面试算法经典-Java实现】【036-Valid Sudoku(验证数独棋盘)】

【036-Valid Sudoku(验证数独棋盘)】


【LeetCode-面试算法经典-Java实现】【所有题目目录索引】

原题

  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.

题目大意

  验证一个数独棋盘是否合法,数独棋盘的验证规则见链接对应的页面。

  数独棋盘是部分填满的,空的位置使用点来代替。

  注意:合法的棋盘不一定要求的可解的,只要填充的数字满足要求就可以。

解题思路

  先对行进行检查,再对列进行检查,最后检查3*3的方格。

代码实现

算法实现类

public class Solution {
    public boolean isValidSudoku(char[][] board) {
        // .的ASCII值是46,0的ASCII值是48,/的ASCII值是47
        int number = board[0].length;
        int[] record = new int[10 + 2]; //保存.到9的值,保存数据的位置在[2, 10]
        boolean isValid;
        reset(record);

        // 对行进行检查
        for (int i = 0; i < number; i++) {
            for (int j = 0; j < number; j++) {
                record[board[i][j] - ‘.‘]++;
            }

            if (!check(record)) { // 如是检查失败
                return false;
            } else { // 检查成功重置棋盘
                reset(record);
            }
        }

        // 对列进行检查
        for (int i = 0; i < number; i++) {
            for (int j = 0; j < number; j++) {
                record[board[j][i] - ‘.‘]++;
            }

            if (!check(record)) { // 如是检查失败
                return false;
            } else { // 检查成功重置棋盘
                reset(record);
            }
        }

        // 检查3*3方块
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {

                for (int k = i * 3; k < (i + 1) * 3; k++) {
                    for (int l = j * 3; l < (j + 1) * 3; l++) {
                        record[board[k][l]- ‘.‘]++;
                    }
                }

                if (!check(record)) { // 如是检查失败
                    return false;
                } else { // 检查成功重置棋盘
                    reset(record);
                }
            }
        }
        return true;
    }

    private void reset(int[] a) {
        for (int i = 0; i < a.length; i++) {
            a[i] = 0;
        }

    }

    /**
     * 检查棋盘一行,一列,或者3*3的方格是否合法,如果1-9中的数字个数大于1就不合法
     *
     * @param a 验证数字
     * @return 返回结果
     */
    private boolean check(int[] a) {
        for (int i = 2; i < a.length; i++) {
            if (a[i] > 1) {
                return false;
            }
        }
        return true;
    }
}

评测结果

  点击图片,鼠标不释放,拖动一段位置,释放后在新的窗口中查看完整图片。

特别说明

欢迎转载,转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47079373

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-28 04:00:12

【LeetCode-面试算法经典-Java实现】【036-Valid Sudoku(验证数独棋盘)】的相关文章

[LeetCode] 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

【LeetCode-面试算法经典-Java实现】【032-Longest Valid Parentheses(最长有效括号)】

[032-Longest Valid Parentheses(最长有效括号)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid paren

【LeetCode-面试算法经典-Java实现】【125-Valid Palindrome(回文字验证)】

[125-Valid Palindrome(回文字验证)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, "A man, a plan, a canal: Panama" is a palindrome. &quo

[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-面试算法经典-Java实现】【225-Implement Stack using Queues(用队列实现栈操作)】

[225-Implement Stack using Queues(用队列实现栈操作)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 代码下载[https://github.com/Wang-Jun-Chao] 原题 Implement the following operations of a stack using queues. push(x) – Push element x onto stack. pop() – Removes the element on

【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】

[139-Word Break(单词拆分)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, given s = "leetcode", di

【LeetCode-面试算法经典-Java实现】【151-Evaluate Reverse Polish Notation(计算逆波兰式)】

[151-Evaluate Reverse Polish Notation(计算逆波兰式)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some

【LeetCode-面试算法经典-Java实现】【019-Remove Nth Node From End of List(移除单链表的倒数第N个节点)】

[019-Remove Nth Node From End of List(移除单链表的倒数第N个节点)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After remo

【LeetCode-面试算法经典-Java实现】【020-Valid Parentheses(括号验证)】

[020-Valid Parentheses(括号验证)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and &qu

【LeetCode-面试算法经典-Java实现】【098-Validate Binary Search Tree(验证二叉搜索树)】

[098-Validate Binary Search Tree(验证二叉搜索树)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys le