[leetcode]Valid Sudoku 解题报告 C 语言

【题目】

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 ‘.’.

Note:

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

【题目分析】

这道题有多种解法,可以花费空间换取时间,也可以花费时间换空间。

第1种方法是一次进行两重循环,每遍历一个数,那么将这个数作为下标之一放到对应的检测数组中去,共有3个数组,检测行、列、小方格。

【具体代码如下】

bool isValidSudoku(char** board, int boardRowSize, int boardColSize) {
    if((boardRowSize!=9)||(boardColSize!=9))return false;
    int flagbox[9][10]={0};
    int flagCol[9][10]={0};
    int flagRow[10]={0};
    int i,j,k;
    int tmp;
    for(i=0;i<boardRowSize;i++)
    {

        for(j=0;j<boardColSize;j++)
        {
            if(board[i][j]!=‘.‘)
            {
            tmp=board[i][j]-‘0‘;
            if(flagRow[tmp]==0)flagRow[tmp]=1;
            else return false;
            if(flagCol[j][tmp]==0)flagCol[j][tmp]=1;
            else return false;
            if(flagbox[i/3*3+j/3][tmp]==0)flagbox[i/3*3+j/3][tmp]=1;
            else return false;
            }

        }
            for(k=0;k<10;k++)
        {
            flagRow[k]=0;
        }

    }
    return true;

}

【个人总结】

这样,只需要进行一次双重循环就可以将行,列,小格子验证完毕。

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

时间: 2024-10-16 19:24:48

[leetcode]Valid Sudoku 解题报告 C 语言的相关文章

LeetCode: Valid Sudoku 解题报告

Valid SudokuDetermine 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]Valid Anagram解题报告 C语言

[题目] Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = "anagram", t = "nagaram", return true. s = "rat", t = "car", return false. Note: You may assume the string cont

LeetCode: Valid Parentheses 解题报告

Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" a

LeetCode: Valid Palindrome 解题报告

Valid Palindrome 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."race a car" is not a palindrome. Note:Have you

LeetCode: Valid Number 解题报告

Valid NumberValidate if a given string is numeric. Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => trueNote: It is intended for the problem statement to be ambi

[LeetCode]Valid Sudoku解题记录

这道题考查对二维数组的处理,哈希表. 1.最自然的方法就是分别看每个数是否符合三个规则,所以就需要相应的数据结构来 记录这些信息,判定是否存在,显然最先想到用哈希表. 2.学会把问题抽象成一个个的子问题. 3.在索引的构建上下工夫. 4.底层数组如何对应的细节没有那么重要,重要的是构成了问题的全集. 代码:这里 附图:一趟遍历时根据i,j,对应到具体的grid,这里的构造模式有多种(??)

[leetcode]Isomorphic Strings 解题报告 C语言

[题目] Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be replaced with another character while preserving the order of charact

[leetcode]Ugly Number 解题报告 C语言

[题目] Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7. Note that 1

[leetcode]Count Primes 解题报告 C语言

[题目] Count the number of prime numbers less than a non-negative number, n. Credits: Special thanks to @mithmatt for adding this problem and creating all test cases. [题目分析] 这道题常用的判断一个数是否为质数是行不通的,根据hint,采用Sieve of Eratosthenes算法实现,具体关于该算法详见https://en.w