题目要求: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. AE(After Effects)的简单使用——记一次模板套用的过程

    一.环境配置 操作系统:win10 64位 软件版本:AE16.11版 二.模板下载 可去 newcger 网站进行模板下载,常用都是免费的,很好用,很推荐 三.具体操作 1.模板素材替换 点击[文件 ...

  2. Serilog 源码解析——Sink 的实现

    在上一篇中,我们简单地查看了 Serilog 的整体需求和大体结构.从这一篇开始,本文开始涉及 Serilog 内的相关实现,着重解决第一个问题,即 Serilog 向哪里写入日志数据的.(系列目录) ...

  3. 为什么要谨慎使用Arrays.asList、ArrayList的subList?

    1. 使用Arrays.asList的注意事项 1.1 可能会踩的坑 先来看下Arrays.asList的使用: List<Integer> statusList = Arrays.asL ...

  4. leetcode143zigzag-conversion

    题目描述 字符串"PAYPALISHIRING"写成3行的Z字形的样式如下: P A H N↵A P L S I I G↵Y I R 按行读这个Z字形图案应该是 "PAH ...

  5. JS中的Array之方法(1)

    a=[2,4,5,6,7,90]; [1]. a.toString();  // 返回字符串表示的数组,逗号分隔 "2,4,5,6,7,90" [2]. a.join('||'); ...

  6. JS多物体宽度运动案例

    任务 对于每一个Div区块,鼠标移入,宽度逐渐变宽,最宽值为400px,当鼠标移除时,宽度逐渐减小,最小值为100px. 任务提示: (1)多物体运动的定时器需要需要每个物体上同时最多只能开一个定时器 ...

  7. Effective Modern C++ ——条款6 当auto型别不符合要求时,使用带显式型别的初始化物习惯用法

    类的代理对象 其实这部分内容主要是说明了在STL或者某些其他代码的容器中,在一些代理类的作用下使得最后的返回值并不是想要的结果. 而他的返回值则是类中的一个容器,看下面的一段代码: std::vect ...

  8. GSO和TSO

    http://www.cnhalo.net/2016/09/13/linux-tcp-gso-tso/ TSO(TCP Segmentation Offload): 是一种利用网卡来对大数据包进行自动 ...

  9. #paragma详解

       #Pragma是预处理指令,它的作用是设定编译器的状态或者是指示编译器完成一些特定的动作.#Pragma指令对每个编译器给出了一个方法,在保持与C和C++语言完全兼容的情况下,给出主机或操作系统 ...

  10. PHP中的变量覆盖漏洞

    简介 今天利用周六整理了一下 php覆盖的漏洞 榆林学院的同学可以使用协会内部实验平台进行实验操作. 1.extract()变量覆盖 1.extract() extract() 函数从数组中将变量导入 ...