【Leetcode】36. Valid Sudoku

原创 2018年04月17日 20:32:54

数独,也很有意思。。

思路挺简单的,横向行检测,纵向列检测,九个方块区域检测。

实现起来就块检测那个地方有点烦。

#include <iostream>
#include <vector>
using namespace std;
bool notRepeat(vector<char> &group)
{
    int nums[9] = {0};
    for (int i = 0; i < 9; i++)
    {
        if (group[i] != '.')
            nums[group[i] - '0' - 1]++;
    }
    for (int i = 0; i < 9; i++)
    {
        if (nums[i] > 1)
            return false;
    }
    return true;
}
bool isValidSudoku(vector<vector<char>> &board)
{
    vector<char> row;
    for (int i = 0; i < 9; i++)
    {
        if (!notRepeat(board[i]))
            return false;
    } //row check
    for (int j = 0; j < 9; j++)
    {
        row.clear();
        for (int i = 0; i < 9; i++)
        {
            row.push_back(board[i][j]);
        }
        if (!notRepeat(row))
            return false;
    } //col check
    //area check  
    for (int kuai = 0; kuai < 9; kuai++)
    {
        vector<char> area;
        int row = kuai / 3;
        int col = kuai % 3;
        area.clear();
        for (int i = row * 3, count1 = 3; count1 > 0; count1--, i++)
        {
            for (int j = col * 3, count2 = 3; count2 > 0; count2--, j++)
            {
                area.push_back(board[i][j]);
            }
        }
        if (!notRepeat(area))
            return false;
    }
    return true;
}
int main()
{
    vector<char> x1 = { '5', '3', '.', '.', '7', '.', '.', '.', '.' };
    vector<char> x2 = { '6', '.', '.', '1', '9', '5', '.', '.', '.' };
    vector<char> x3 = { '.', '9', '8', '.', '.', '.', '.', '6', '.' };
    vector<char> x4 = { '8', '.', '.', '.', '6', '.', '.', '.', '3' };
    vector<char> x5 = { '4', '.', '.', '8', '.', '3', '.', '.', '1' };
    vector<char> x6 = { '7', '.', '.', '.', '2', '.', '.', '.', '6' };
    vector<char> x7 = { '.', '6', '.', '.', '.', '.', '2', '8', '.' };
    vector<char> x8 = { '.', '.', '.', '4', '1', '9', '.', '.', '5' };
    vector<char> x9 = { '.', '.', '.', '.', '8', '.', '.', '7', '9' };
    vector<vector<char>> shudu = {x1, x2, x3, x4, x5, x6, x7, x8, x9};
    if(isValidSudoku(shudu))
        cout << "True" << endl;
    else
        cout << "False" << endl;
    return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/syc666946/article/details/79977281

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

Problem: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku boa...
  • runningtortoises
  • runningtortoises
  • 2015-05-18 22:56:46
  • 3120

LeetCode(36)Valid Sudoku

题目Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be...
  • fly_yr
  • fly_yr
  • 2015-08-25 20:40:50
  • 2443

leetcode 36: Valid Sudoku

Total Accepted: 24574 Total Submissions: 90344 Determine if a Sudoku is valid, accordi...
  • xudli
  • xudli
  • 2015-01-29 12:59:05
  • 2338

LeetCode-36. Valid Sudoku (JAVA)(有效数独)

LeetCode-36. Valid Sudoku (JAVA)(有效数独)
  • mine_song
  • mine_song
  • 2017-04-17 09:25:04
  • 643

36. Valid Sudoku [easy] (Python)

题目链接https://leetcode.com/problems/valid-sudoku/题目原文 Determine if a Sudoku is valid, according to: ...
  • coder_orz
  • coder_orz
  • 2016-06-06 16:35:20
  • 1590

python写算法题:leetcode: 36. Valid Sudoku

https://leetcode.com/problems/valid-sudoku/description/ 测试用例少量未通过,分析原因,本程序判断条件更严格,有些明显存在矛盾填写方式,被认定非...
  • lzf_china
  • lzf_china
  • 2017-12-12 01:14:37
  • 66

LeetCode 36 Valid Sudoku(有效数独)

翻译数独板被部分填充,空格部分用'.'来填充。一个部分填充的数组是否有效只需要看其填充的部分即可。原文代码这道题写了一会,错了……因为输入太懒搞了,就直接看了别人写的……class Solution ...
  • NoMasp
  • NoMasp
  • 2015-11-30 20:58:35
  • 3591

Leetcode_36_Valid Sudoku

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42497857 Determine if a Su...
  • pistolove
  • pistolove
  • 2015-01-08 19:15:35
  • 1469

[leetcode] 【数组】36. Valid Sudoku

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be p...
  • u014654002
  • u014654002
  • 2016-05-22 20:04:55
  • 125

【leetcode-36】valid sudoku(java)

问题描述: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board cou...
  • zdavb
  • zdavb
  • 2015-07-23 19:57:54
  • 414
收藏助手
不良信息举报
您举报文章:【Leetcode】36. Valid Sudoku
举报原因:
原因补充:

(最多只允许输入30个字)