LeetCode_Valid Sudoku

一.题目

Valid Sudoku

Total Accepted: 29804 Total
Submissions: 109584My
Submissions

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.

Show Tags

Have you met this question in a real interview?

Yes

No

Discuss

二.解题技巧

这道题比较简单,就是遍历棋盘的所有位置,查看每一个位置上的元素是否满足Sudoku的要求,也就是每一行,每一列以及每个小9×9的区域是否存在相同的元素,如果存在相同的元素,即为不合法。对于判断一个元素是否在某一行,某一列或者某个小区域内,我的实现是将定义了9个整数代表每一列,9个整数代表每一行,9个元素代表了9个小的9×9的区域,因此,用每一个整数的第1到第九位表示某个元素是否在该列,该行或该小区域中。通过位操作可以判断该元素是否存在。

三.实现代码

#include <iostream>

#include <vector>

using namespace std;

class Solution
{
private:
    bool isExist(int &Input, int Number)
    {
        if (Input & (1 << Number))
        {
            return true;
        }

        Input =  Input | (1 << Number);
        return false;
    }

public:
    bool isValidSudoku(vector<vector<char> > &board)
    {
        int X_value[9] = {0};
        int Y_value[9] = {0};
        int Inside[9] = {0};

        for (int Index_y = 0; Index_y < 9; Index_y++)
        {
            vector<char> &TmpRow = board[Index_y];

            for (int Index_x = 0; Index_x < 9; Index_x++)
            {
                if (TmpRow[Index_x] == '.')
                {
                    continue;
                }

                int TmpValue = TmpRow[Index_x] - '0';

                // is valid in Index_x row
                if (isExist(X_value[Index_x], TmpValue))
                {
                    return false;
                }

                // is valid in Index_y col
                if (isExist(Y_value[Index_y], TmpValue))
                {
                    return false;
                }

                // is valid in 3*3 sub block
                if (isExist(Inside[Index_x / 3 + Index_y / 3 * 3] , TmpValue))
                {
                    return false;
                }
            }
        }

        return true;

    }
};

四.体会

这道题基本上不需要什么算法,只是在考虑如何判断一个元素是否已经存在时需要稍微考虑下,我的实现用来27个整数的第1到第9位来判断一个元素是否已经存在。其实使用三个整数就基本上可以完成这样的任务了,只不过这样处理起来稍微有点复杂。

这道题也算是一道水题,可以用于练练手。

版权所有,欢迎转载,转载请注明出处,谢谢

时间: 2024-08-25 02:21:18

LeetCode_Valid Sudoku的相关文章

LeetCode37 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. A sudoku puzzle... ...and its solution numbers marked in red.  (Hard)

*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. A sudoku puzzle... ...and its solution numbers marked in red. public clas

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]Valid Sudoku

检测数独是否合格. 思路: 填充一遍就知道是否合格. 基本暴力搜索的思想. 1 /*************************************************************************************************** 2 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. 3 The Sudoku board could be parti

POJ Sudoku 数独填数 DFS

题目链接:Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18105   Accepted: 8772   Special Judge Description Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smaller squares 3x3 as shown on the Fig

Valid Sudoku leetcode

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. 判断九宫格的合理性(并不一定有解),只需要依次判断行.列.9个子九宫格是否

LeetCode OJ: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 '.'. 注意这里的有效数独并非指的是可以解出来,只要存在的数满足数独的条件就可以了. 原理很简单,但是判定在同一个blocks的时候出了点问题,没想到判定方法,看了下

Java [leetcode 37]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. A sudoku puzzle... ...and its solution numbers marked in red. 解题思路:

2015南阳CCPC H - Sudoku 暴力

H - Sudoku Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description Yi Sima was one of the best counselors of Cao Cao. He likes to play a funny game himself. It looks like the modern Sudoku, but smaller. Actually, Yi Sima was playing it different. F