题目要求: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.

分析:

本题是让写数独的check函数(isValidSudoku)~基本有三个步骤:

① 每一行都合法;

② 每一列都合法;

③ 每一块都合法(3 * 3)。

代码如下:

class Solution {
public:
bool isValidSudoku(vector<vector<char> > &board) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
return
isValidRow(board) && isValidColumn(board) && isValidBox(board);
} private: bool isValidRow(vector<vector<char> > &board) {
int count[9];
for (int i = 0; i < 9; i++) {
memset(count, 0, sizeof(int) * 9);
for (int j = 0; j < 9; j++) {
if (!add(count, board[i][j])) {
return false;
}
}
}
return true;
} bool isValidColumn(vector<vector<char> > &board) {
int count[9];
for (int i = 0; i < 9; i++) {
memset(count, 0, sizeof(int) * 9);
for (int j = 0; j < 9; j++) {
if (!add(count, board[j][i])) {
return false;
}
}
}
return true;
} bool isValidBox(vector<vector<char> > &board) {
int point[9][2] = {
{1, 1}, {1, 4}, {1, 7}, {4, 1}, {4, 4}, {4, 7}, {7, 1}, {7, 4}, {7, 7}
};
int dir[8][2] = {
{-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}, {-1, -1}
};
int count[10]; for (int i = 0, x, y; i < 9; i++) {
memset(count, 0, sizeof(int) * 10);
x = point[i][0];
y = point[i][1];
add(count, board[x][y]);
for (int j = 0; j < 8; j++) {
if (!add(count, board[dir[j][0] + x][dir[j][1] + y])) {
return false;
}
}
}
return true;
} bool add(int count[], char c) {
if (c == '.') {
return true;
} //如果每个字符出现次数 <= 1,则正常+_+
return (++count[c - '1']) <= 1;
}
};

简化版本:

class Solution {
public:
bool isValidSudoku(vector<vector<char> > &board) {
// Note: The Solution object is instantiated only once.
vector<vector<bool>> rows(9, vector<bool>(9,false));
vector<vector<bool>> cols(9, vector<bool>(9,false));
vector<vector<bool>> blocks(9, vector<bool>(9,false)); for(int i = 0; i < 9; i++)
for(int j = 0; j < 9; j++){
if(board[i][j] == '.')continue; //rows, cols, blocks分别是9个布尔值
//将每个点分别赋值为true
//若未赋值的为true了,则有问题
int num = board[i][j] - '1';
if(rows[i][num] || cols[j][num] || blocks[i - i%3 + j/3][num])
return false;
rows[i][num] = cols[j][num] = blocks[i - i%3 + j/3][num] = true;
}
return true;
} };

LeetCode 036 Valid Sudoku的更多相关文章

  1. [LeetCode] 036. Valid Sudoku (Easy) (C++)

    指数:[LeetCode] Leetcode 解决问题的指数 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 036. ...

  2. Java for LeetCode 036 Valid Sudoku

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...

  3. LeetCode:36. Valid Sudoku,数独是否有效

    LeetCode:36. Valid Sudoku,数独是否有效 : 题目: LeetCode:36. Valid Sudoku 描述: Determine if a Sudoku is valid, ...

  4. 【LeetCode】036. Valid Sudoku

    题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  5. LeetCode 36 Valid Sudoku

    Problem: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board ...

  6. 【leetcode】Valid Sudoku

    题目简述: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board cou ...

  7. Java [leetcode 36]Valid Sudoku

    题目描述: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board cou ...

  8. LeetCode(38)-Valid Sudoku

    题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  9. [leetcode] 20. Valid Sudoku

    这道题目被放在的简单的类别里是有原因的,题目如下: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. ...

随机推荐

  1. 系统日志报错i8042prt无法加载

    原因如下: 解决方法为: 此报错可以直接忽略,不过由此可能导致即插即用(plugplay)报错,在即插即用报错时,重启服务器即可.

  2. 神州笔记本电脑【K670D】安装 Ubuntu18.04 系列操作

    一.使用U盘安装 Ubuntu 前的处理如下: 进入BIOS将SSD设为启动首选项 -> F10保存退出 -> 用方向键高亮ubuntu启动项 -> 按e键进入编辑状态 -> ...

  3. Java的浅拷贝与深拷贝

    Java的浅拷贝与深拷贝 Java中,所有的类都继承Object,Object中有clone方法,它被声明为了 protected ,所以我们但是如果要使用该方法就得重写且声明为public,必须在要 ...

  4. 使用docker 部署codis

    使用docker 部署codis 原文地址:https://www.jianshu.com/p/85e72ae6fec3 codis的架构图 1.zookeeeper,用于存放统一配置信息和集群状态 ...

  5. redis的rdb与aof持久化机制

    Redis提供了两种持久化方案:RDB持久化和AOF持久化,一个是快照的方式,一个是类似日志追加的方式 RDB快照持久化 RDB持久化是通过快照的方式,即在指定的时间间隔内将内存中的数据集快照写入磁盘 ...

  6. How to using expression setup BackgroundColor AX2012 SSRS Report[AX2012]

    tile label using [#99ccff] property BackgroundColor - > expression =Iif(Fields!Flag.Value = " ...

  7. MVC中Cookie的用法(二)---CookieHelper

    public class CookieHelper { /// <summary> /// 1.1添加Cookie /// </summary> /// <param n ...

  8. 为什么继承 Python 内置类型会出问题?!

    本文出自"Python为什么"系列,请查看全部文章 不久前,Python猫 给大家推荐了一本书<流畅的Python>(点击可跳转阅读),那篇文章有比较多的"溢 ...

  9. kali xHydra使用

    简介: Hydra是一款登录爆破神器,Hydar几乎可以爆破各种协议的登录,比如windows的远程桌面.ssh.ftp.路由交换设备等等. Hydar在kali linux默认已经安装. 大概介绍一 ...

  10. python_super()及继承顺序

    class A(object): def func(self): print('A') class B(A): def func(self): super().func() print('B') cl ...