LeetCode 36 Valid Sudoku(有效数独)(*)

翻译

数独板被部分填充,空格部分用'.'来填充。

一个部分填充的数组是否有效只需要看其填充的部分即可。

原文

代码

这道题写了一会,错了……因为输入太懒搞了,就直接看了别人写的……

class Solution {
public:
    int a[9];
    bool isValidSudoku(vector<vector<char>>& board) {
        memset(a,0,sizeof(a));
        for (int i=0,j=0,row=0,col=0;i<9;++j,j==9?++i,j=0,row=col=0:0)
        {
            if (board[i][j]!='.')
            {
                if ((1<<board[i][j]-48) & row) return false;
                else row|=1<<board[i][j]-48;
                if ((1<<board[i][j]-48) & a[i/3*3+j/3]) return false;
                else a[i/3*3+j/3]|=1<<board[i][j]-48;
            }
            if (board[j][i]!='.')
                if ((1<<board[j][i]-48) & col) return false;
                else col|=1<<board[j][i]-48;
        }
        return true;
    }
};
class Solution {
public:
    bool isValidSudoku(vector<vector<char>>& board) {
        int i, j, c;
        int row[9][9], col[9][9], block[3][3][9];
        memset(row, 0, sizeof(row));
        memset(col, 0, sizeof(col));
        memset(block, 0, sizeof(block));
        for (i = 0; i < 9; i++) {
            for (j = 0; j < 9; j++) {
                if(board[i][j] != '.'){
                    c = board[i][j] - '1';
                    if (row[i][c] || col[j][c] || block[i / 3][j / 3][c])
                        return false;
                    else {
                        row[i][c] ++;
                        col[j][c] ++;
                        block[i / 3][j / 3][c]++;
                    }
                }
            }
        }
        return true;
    }
};
时间: 2024-09-20 18:48:28

LeetCode 36 Valid Sudoku(有效数独)(*)的相关文章

[LeetCode]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. [题意] 假设一个数独是有效的,则它符合数独游戏 规则(点击打开

【LeetCode从零单排】No36 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 vali

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 (partiall

[LeetCode]125.Valid Palindrome

[题目] Valid Palindrome  Total Accepted: 3479 Total Submissions: 16532My Submissions 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 pa

[LeetCode] 20.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 "(]" and "([)]

[LeetCode]65.Valid Number

题目 Validate if a given string is numeric. Some examples: "0" => true " 0.1 " => true "abc" => false "1 a" => false "2e10" => true Note: It is intended for the problem statement to be ambiguo

leetcode 20 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 "(]" and "([)]"

LeetCode 20 Valid Parentheses(有效的括号)

翻译 给定一个只包含'(', ')', '{', '}', '[' 和']'的字符串,判断这个输入的字符串是否是有效的. 括号必须在正确的形式下闭合,"()" 和"()[]{}" 是有效的,但是 "(]" 和"([)]" 则不是. 原文 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the

[LeetCode]--242. Valid Anagram

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 contains