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

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.

题解:

Solution 1

class Solution {
public:
    bool isValidSudoku(vector<vector<char>>& board) {
        const int n = 9;

        for(int i = 0; i < n; ++i){
            bool used[n] = {false};
            for(int j = 0; j < n; ++j){
                if(isused(board[i][j], used))
                    return false;
            }
        }
        for(int i = 0; i < n; ++i){
            bool used[n] = {false};
            for(int j = 0; j < n; ++j){
                if(isused(board[j][i], used))
                    return false;
            }
        }
        for(int r = 0; r < 3; ++r){
            for(int c = 0; c < 3; ++c){
                bool used[n] = {false};
                for(int i = r * 3; i < r * 3 + 3; ++i){
                    for(int j = c * 3; j < c * 3 + 3; ++j){
                        if(isused(board[i][j], used))
                           return false;
                    }
                }
            }
        }
        return true;
    }
    bool isused(char val, bool used[9]){
        if(val == ‘.‘)
            return false;
        if(used[val - ‘1‘])
            return true;
        used[val - ‘1‘] = true;
        return false;
    }
};    

Solution 2

class Solution {
public:
    bool isValidSudoku(vector<vector<char>>& board) {
        const int n = 9;
        vector<vector<bool>> rowboard(n, vector<bool>(n, false));
        vector<vector<bool>> colboard(n, vector<bool>(n, false));
        vector<vector<bool>> celboard(n, vector<bool>(n, false));
        for(int i = 0; i < n; ++i){
            for(int j = 0; j < n; ++j){
                if(board[i][j] == ‘.‘) continue;
                int ch = board[i][j] - ‘1‘;
                if(rowboard[i][ch] || colboard[ch][j] || celboard[3 * (i / 3) + j / 3][ch])
                    return false;
                rowboard[i][ch] = true;
                colboard[ch][j] = true;
                celboard[3 * (i / 3) + j / 3][ch] = true;
            }
        }
        return true;
    }
};
时间: 2024-11-08 17:11:42

【Leetcode】036. Valid Sudoku的相关文章

【LeetCode】36 - 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 '.'. A partially filled sudoku which is valid. N

【LeetCode】Longest Valid Parentheses 解题报告

[题目] Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example

【LeetCode】242. Valid Anagram (2 solutions)

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 strin

【leetcode】Longest Valid Parentheses

Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length

【LeetCode】125 - 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 consider that th

【LeetCode】20. Valid Parentheses

题目: 思路:用Stack基本操作完成. 要检测输入字符是否满足这个条件,一个非常合适的数据结构是stack,后进先出的特征正好满足检测的需求.在检测的时候,每次检查一个字符,如果是左括号,就入栈,如果是右括号,并且右括号和当前栈顶符号是左右配对的,那么就弹出栈顶并且进行下一次检测,如果不满足上面两种情况,就说明检查到了一个非法字符,返回false. public class Solution { public boolean isValid(String s) { if(s.length()=

【leetcode】367. Valid Perfect Square]

题目描述: Given a positive integer num, write a function which returns True if num is a perfect square else False. 解题分析: 这种找数字的题一般都用类似与二分查找的算法.需要注意的是比较平方和时考虑到integer溢出的情况.所以这个结果是要用Long类型保存.由此到来的改变是判断相等时要用“equals()”方法,而不是“==”. 实现代码: 1 public class Solutio

【leetcode 】20. Valid Parentheses

这题需要注意: 栈的空指针 全局变量stack需要clear 输入结束时栈长度不为空 public class Solution { private static final Stack<Character> stack = new Stack<Character>(); public boolean isValid(String s) { stack.clear(); for (int i = 0; i < s.length(); ++i) { char c = s.cha

【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