description:

判断数独是否正确
Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:

Each row must contain the digits 1-9 without repetition.
Each column must contain the digits 1-9 without repetition.
Each of the 9 3x3 sub-boxes of the grid must contain the digits 1-9 without repetition.

Note:

A Sudoku board (partially filled) could be valid but is not necessarily solvable.
Only the filled cells need to be validated according to the mentioned rules.
The given board contain only digits 1-9 and the character '.'.
The given board size is always 9x9.

Example:

Example 1:Input:
[["5","3",".",".","7",".",".",".","."],["6",".",".","1","9","5",".",".","."],[".","9","8",".",".",".",".","6","."],["8",".",".",".","6",".",".",".","3"],["4",".",".","8",".","3",".",".","1"],["7",".",".",".","2",".",".",".","6"],[".","6",".",".",".",".","2","8","."],[".",".",".","4","1","9",".",".","5"],[".",".",".",".","8",".",".","7","9"]
]
Output: trueExample 2:Input:
[["8","3",".",".","7",".",".",".","."],["6",".",".","1","9","5",".",".","."],[".","9","8",".",".",".",".","6","."],["8",".",".",".","6",".",".",".","3"],["4",".",".","8",".","3",".",".","1"],["7",".",".",".","2",".",".",".","6"],[".","6",".",".",".",".","2","8","."],[".",".",".","4","1","9",".",".","5"],[".",".",".",".","8",".",".","7","9"]
]
Output: false
Explanation: Same as Example 1, except with the 5 in the top left corner being modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid.

answer:

class Solution {
public:bool isValidSudoku(vector<vector<char>>& board) {if (board.empty() || board[0].empty()) return false;vector<vector<bool>> rowFlag(9, vector<bool>(9, false));vector<vector<bool>> colFlag(9, vector<bool>(9, false));vector<vector<bool>> cellFlag(9, vector<bool>(9, false));for (int i = 0; i < 9; i++){for (int j = 0; j < 9; j++) {if (board[i][j] >= '1' && board[i][j] <= '9'){int c = board[i][j] - '1';if (rowFlag[i][c] || colFlag[c][j] || cellFlag[3 * (i / 3) + j / 3][c]) return false;rowFlag[i][c] = true;colFlag[c][j] = true;cellFlag[3 * (i / 3) + j / 3][c] = true; // 这里对小方块的坐标转换很关键,首先 (i/3) 是确定这个数所在的小方块所在第几行(0/1/2)// 然后再乘以3是算出这个数之前几行有几个方块,在加上此列前面几个,就是算出来了这个数到底在第几个方块。} }}return true;}
};

relative point get√:

hint :

其实是对这个答案有疑问的:

  • 没有判断如果是个不合法的数独形式怎么办
  • 没有判断数字不是1-9内的怎么办

转载于:https://www.cnblogs.com/forPrometheus-jun/p/11116372.html

36. Valid Sudoku相关推荐

  1. leetcode 36. Valid Sudoku | 37. Sudoku Solver(数独)

    36. Valid Sudoku https://leetcode.com/problems/valid-sudoku/ 题解 class Solution {public boolean isVal ...

  2. LeetCode 36. Valid Sudoku

    LeetCode 36. Valid Sudoku Solution1:我的答案,比较笨.但是清晰易懂好上手啊~~~ class Solution { public:bool isValidSudok ...

  3. 36. Valid Sudoku数独判断

    题目:数独填写正确判断 https://leetcode.com/problems/valid-sudoku/description/ Determine if a Sudoku is valid, ...

  4. [leetcode]36. Valid Sudoku c语言

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

  5. LeetCode 36. Valid Sudoku(九宫格数独)

    依次检查每行,每列,每个子九宫格是否出现重复元素,如果出现返回false,否则返回true. 难点在于表示第i个九宫格每个格点的坐标. 观察行号规律: 第0个九宫格:000111222; 第1个九宫格 ...

  6. 【LeetCode】36. Valid Sudoku

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

  7. 36. 有效的数独(javascript)36. Valid Sudoku

    请你判断一个 9 x 9 的数独是否有效.只需要 根据以下规则 ,验证已经填入的数字是否有效即可. 数字 1-9 在每一行只能出现一次. 数字 1-9 在每一列只能出现一次. 数字 1-9 在每一个以 ...

  8. LeetCode 36 Valid Sudoku(有效数独)(*)

    翻译 数独板被部分填充,空格部分用'.'来填充.一个部分填充的数组是否有效只需要看其填充的部分即可. 原文 代码 这道题写了一会,错了--因为输入太懒搞了,就直接看了别人写的-- class Solu ...

  9. Valid Sudoku leetcode java

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

最新文章

  1. 通过使用Byte Buddy,便捷地创建Java Agent
  2. C++ 是 编程界 的 背锅侠
  3. java后台日期怎么去重,JAVA后台业务实现去重
  4. VTK:vtkClipClosedSurface用法实战
  5. java testng 优化_java+testNG测试框架搭建----jenkins自动化执行
  6. 程序员深夜啪啪啪真相,看完笑翻!
  7. java shiro登录实例_使用Shiro实现登录成功后跳转到之前的页面
  8. Sphinx 文档例子
  9. 话里话外:谁才是流程的主人
  10. verilog奇偶分频
  11. java 字符串索引从0开始_无限字符串中的字符串的第一个索引-Java
  12. STM32命名规则 STM32选型手册
  13. 色差大调色难?实操讲解如何去除谷歌影像色差
  14. 一个方便快捷gif在线水印制作(支持文字和图片)
  15. 软件介绍——SyncToy 微软官方文件同步工具
  16. Java之非对称加密
  17. 阅读不懂,图书之过——《大话设计模式》创作历程
  18. 会计专业与计算机专业结合复合型,我国会计电算化的现状、问题及对策
  19. 战略游戏——树形dp+状态机——没有上司的舞会翻版
  20. iOS图像渲染 + 动画探索

热门文章

  1. 【CHARINDEX】先按STATIC_VALUE排序 在按R_RECORD_CREATE_DATE排序
  2. 深入Eureka/Feign/Hystrix原理学习(1)
  3. Java多线程系列--“基础篇”09之 interrupt()和线程终止方式
  4. TreeMap实现权重随机数Java
  5. Java并发编程实战 第13章 显式锁
  6. 【Unity3D】Tags和Layers
  7. Automatic IE Testing With Python
  8. c#在线程中打开类似msn的消息提示窗口
  9. ubuntu10.4安装交叉编译器arm-none-linux-gnueabi-gcc
  10. 9.2-3 pstree pgrep