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 '.'.

注意这里的有效数独并非指的是可以解出来,只要存在的数满足数独的条件就可以了。

原理很简单,但是判定在同一个blocks的时候出了点问题,没想到判定方法,看了下别人的,很精妙,代码如下:

 1 class Solution {
2 public:
3 bool isValidSudoku(vector<vector<char>>& board) {
4 vector<vector<bool>> rowValid(9, vector<bool>(9, true));
5 vector<vector<bool>> colValid(9, vector<bool>(9, true));
6 vector<vector<bool>> blockValid(9, vector<bool>(9, true));
7 for (int i = 0; i < 9; ++i){
8 for(int j = 0; j < 9; ++j){
9 if(board[i][j] == '.') continue;
10 int num = board[i][j] - '1';//注意这里是减'1'
11 if(!rowValid[i][num] || !colValid[j][num] || !blockValid[i-i%3+j/3][num])//判定在一个block中的代码很巧妙,i-i%3制造了一个高度为3的阶梯,然后j/3正好可以将这个阶梯填满
12 return false;
13 rowValid[i][num] = colValid[j][num] = blockValid[i-i%3+j/3][num] = false;
14 }
15 }
16 return true;
17 }
18 };

LeetCode OJ:Valid Sudoku(有效数独问题)的更多相关文章

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

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

  2. [LeetCode] 36. Valid Sudoku 验证数独

    Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to th ...

  3. [leetcode]36. Valid Sudoku验证数独

    Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to th ...

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

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

  5. leetCode 36.Valid Sudoku(有效的数独) 解题思路和方法

    Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...

  6. LeetCode 36 Valid Sudoku

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

  7. LeetCode(38)-Valid Sudoku

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

  8. [leetcode] 20. Valid Sudoku

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

  9. 蜗牛慢慢爬 LeetCode 36.Valid Sudoku [Difficulty: Medium]

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

  10. LeetCode 036 Valid Sudoku

    题目要求:Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudo ...

随机推荐

  1. Spring MVC 复习笔记01

    1. springmvc框架 1.1 什么是springmvc spring mvc是spring框架的一个模块,springmvc和spring无需通过中间整合层进行整合.spring mvc是一个 ...

  2. ORA-00257: archiver error的解决方法

    背景:多个用户同时做测试数据,有时候突然Oracle系统就崩溃了,然后报一个ORA-00257: archiver error. Connect internal only, until freed的 ...

  3. 获取Android设备的方向,Sensor和SensorManager实现手机旋转角度

    http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2012/1009/425.html 带有g-sensor的Android设备上可通过API ...

  4. BigDecimal处理加减乘除

    public static void main(String[] args) { BigDecimal totalDoneAmt = new BigDecimal(2); Double d1 = ad ...

  5. 20145309java第三次实验报告

    实验三 敏捷开发与XP实践 实验内容 •下载并学会使用git上传代码: •与同学结对,相互下载并更改对方代码,并上传: •实现代码的重载. 实验步骤 下载并用git上传代码: •1.下载并安装好git ...

  6. USB详解

    USB作为一种串行接口,应用日益广泛.如同每个工程设计人员必须掌握I2C,RS232这些接口一样,我们也必须掌握USB.但是USB的接口协议实在有点费解,Linux UCHI驱动作者之一Alan St ...

  7. 高手用的SourceInsight配置文件——仿Sublime风格【转】

    本文转载自:https://blog.csdn.net/weixin_38233274/article/details/80209100 配置文件下载地址:https://download.csdn. ...

  8. oracle查看被锁的表以及解锁表

    在oracle 上面查看别锁定的表,以及解锁表的sql: select t3.object_name,t3.owner,t2.machine,t2.sid,t2.serial# from v$lock ...

  9. Maven打包命令

    mvn clean 会把原来target目录给删掉重新生成.mvn install 安装当前工程的输出文件到本地仓库,然后打包mvn clean install 先删除target文件夹 ,然后打包到 ...

  10. js添加事件 attachEvent 和addEventListener的用法

    一般我们在JS中添加事件,是这样子的: obj.onclick = method 这种绑定事件的方式,兼容主流浏览器,但是如果一个元素上添加多次同一个事件呢??? obj.onclick = meth ...