题目:

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 = ; for(int i = ; i < n; ++i){
bool used[n] = {false};
for(int j = ; j < n; ++j){
if(isused(board[i][j], used))
return false;
}
}
for(int i = ; i < n; ++i){
bool used[n] = {false};
for(int j = ; j < n; ++j){
if(isused(board[j][i], used))
return false;
}
}
for(int r = ; r < ; ++r){
for(int c = ; c < ; ++c){
bool used[n] = {false};
for(int i = r * ; i < r * + ; ++i){
for(int j = c * ; j < c * + ; ++j){
if(isused(board[i][j], used))
return false;
}
}
}
}
return true;
}
bool isused(char val, bool used[]){
if(val == '.')
return false;
if(used[val - ''])
return true;
used[val - ''] = true;
return false;
}
};

Solution 2

class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
const int n = ;
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 = ; i < n; ++i){
for(int j = ; j < n; ++j){
if(board[i][j] == '.') continue;
int ch = board[i][j] - '';
if(rowboard[i][ch] || colboard[ch][j] || celboard[ * (i / ) + j / ][ch])
return false;
rowboard[i][ch] = true;
colboard[ch][j] = true;
celboard[ * (i / ) + j / ][ch] = true;
}
}
return true;
}
};

【LeetCode】036. Valid Sudoku的更多相关文章

  1. 【LeetCode】36. Valid Sudoku 解题报告(Python)

    [LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...

  2. 【LeetCode】36 - Valid Sudoku

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.(http://sudoku.com.au/TheRu ...

  3. 【leetcode】36. Valid Sudoku(判断能否是合法的数独puzzle)

    Share Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated accordi ...

  4. 【LeetCode】593. Valid Square 解题报告(Python)

    [LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  5. 【LeetCode】678. Valid Parenthesis String 解题报告(Python)

    [LeetCode]678. Valid Parenthesis String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...

  6. 【LeetCode】65. Valid Number

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description Validate if a given string can be interpreted ...

  7. 【LeetCode】680. Valid Palindrome II

    Difficulty:easy  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/valid-palindrome ...

  8. 【一天一道LeetCode】#36. Valid Sudoku

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:https://github.com/Zeecoders/LeetCode 欢迎转载,转载请注明出处 (一)题目 Determi ...

  9. 【LeetCode】941. Valid Mountain Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

随机推荐

  1. c++的检测的确比C++更严格

    见下面代码 #include <stdio.h> #include <stdlib.h> #include <time.h> enum guess { paper, ...

  2. GCD多线程在swift中的变化

    1.异步线程加载主线程刷新 DispatchQueue.global().async { // TODO:执行异步线程网络请求 DispatchQueue.main.async(execute: { ...

  3. iOS-事件传递和响应机制

    转自:http://www.jianshu.com/p/2e074db792ba 前言: 按照时间顺序,事件的生命周期是这样的: 事件的产生和传递(事件如何从父控件传递到子控件并寻找到最合适的view ...

  4. What is the difference between iterations and epochs in Convolution neural networks?

    https://stats.stackexchange.com/questions/164876/tradeoff-batch-size-vs-number-of-iterations-to-trai ...

  5. 牛客小白月赛1 A 简单题 【数学】

    题目链接 https://www.nowcoder.com/acm/contest/85/A 思路 这个 就是 E 但是 运算的时候 要保证 其精度 AC代码 #include <cstdio& ...

  6. JAVA中最方便的Unicode转换方法

    在命令行界面用native2ascii工具  1.将汉字转为Unicode:  C:\Program   Files\Java\jdk1.5.0_04\bin>native2ascii  测试 ...

  7. python 3 封装

    python 3 封装 从封装本身的意思去理解,封装就好像是拿来一个麻袋,把小鱼,小虾,小王八,一起装进麻袋,然后把麻袋封上口子.照这种逻辑看,封装=‘隐藏’,这种理解是相当片面的. 先看如何隐藏 在 ...

  8. OSI模型网络分层

    OSI TCP/IP --- ------- 应用层 表示层 应用层 会话层 ----- ------- 传输层 TCP UDP ----- ------- 网络层 IPv4/IPv6 ----- - ...

  9. Python 变量(赋值,数据类型,数据类型转换)

    一.python 变量赋值方式有三种: 1.直接赋值:age = 28 2.多个变量赋值 age, sex = 28, 1  #每个变量都必须要有个对应的值 3.特殊形式的赋值(链式赋值)  a = ...

  10. delphi的获取某坐标的颜色值

    1.通过API函数GetPixel(),获取某个点的颜色值; var PT : TPoint; C : TColor; DC : HDC; hwd : THandle; I : integer; be ...