leetcode笔记:Valid Sudoku

一.题目描述

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

http://sudoku.com.au/TheRules.aspx .

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

The following figure: A partially filled sudoku which is valid.

二.题目分析

关于Sudoku矩阵的性质,可以简单概括为:对于矩阵中每一行、每一列以及每个3×3的九宫格区域是否存在唯一的0~9排列组合,如果存在相同的元素,则该Sudoku矩阵不合法。

这道题比较简单,就是遍历Sudoku矩阵的所有元素,检查各元素是否满足Sudoku的性质。对于判断一个元素是否在某一行,某一列或者某个小区域内,我定义了一个用于存放0~9数字出现次数的map,使用‘1‘,‘2‘,...,‘9‘作为关键字只要某个关键字的储存对象大于1,表示在该行、列或3×3的九宫格区域中有重复的某个关键字,通过这种方式进行遍历即可判断矩阵是否合法。

三.示例代码

#include <iostream>
#include <vector>
#include <map>
using namespace std;

class Solution
{
public:
    bool isValidSudoku(vector<vector<char> >& board)
    {
        map<char, int> sudokuMap;
        for (int i = 0; i < 9; i++)
        {
            sudokuMap.clear();
            for (int j = 0; j < 9; j++)
            {
                sudokuMap[board[i][j]]++;
                if ((board[i][j] != ‘.‘) && (sudokuMap[board[i][j]] > 1))
                    return false;
            }
        }

        for (int i = 0; i < 9; i++)
        {
            sudokuMap.clear();
            for (int j = 0; j < 9; j++)
            {
                sudokuMap[board[j][i]]++;
                if ((board[j][i] != ‘.‘) && (sudokuMap[board[j][i]] > 1))
                    return false;
            }
        }

        for (int i = 0; i < 9; i += 3)
        {
            for (int j = 0; j < 9; j += 3)
            {
                sudokuMap.clear();
                for (int k = i; k < i + 3; k++)
                {
                    for (int l = j; l < j + 3; l++)
                    {
                        sudokuMap[board[k][l]]++;
                        if ((board[k][l] != ‘.‘) && (sudokuMap[board[k][l]] > 1))
                            return false;
                    }
                }
            }
        }
        return true;
    }
};

运行结果如下:

1.一个正确的数独矩阵:

2.一个错误的数独矩阵:

四.小结

这道题不要求实现什么算法,只需设计一下矩阵运算,使用map用于存放每行、每列或每个九宫格中1~9每个数出现的次数,只要发现有大于一的,即可立即否定这个Sudoku,只有遍历整个Sudoku矩阵且符合要求,才判定为valid sudoku。

相关题目:Sudoku solver

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

时间: 2024-10-26 19:19:27

leetcode笔记: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(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 s

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笔记:Sudoku Solver

一.题目描述 Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution. The following photo is a sudoku puzzle- -and its solution numbers